达内首页 > IT技术学院 > iOS学院 > IOS表格UITableView学习内容详解
IOS表格UITableView学习内容详解
作者:广州达内科技 更新时间:2014-04-21 13:52 来源:Android开发培训

  UITableView是UIScrollView的子类,因此UITableView可以响应滚动事件。

  要使用UITableView需要让控制器实现UITableViewDataSource 协议 或者让控制器 继承 UITableViewController 它已经实现UITableViewDataSource 和 UITableViewDelegate 代理协议

  使用UITableViewDataSource “必须实现的方法” 俗称配置数据源

  // 每一组显示多少行

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  与

  // 每一行显示什么内容 默认当模拟器启动的时候只显示可视范围的单元格(cell)内容数据 当有一个新的cell进入视野范围,就会调用它 创建一个新的单元格(Cell), 当不可视的时候会进行自动释放。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  1.首先在加载的时候要让tableView的数据源与控制器关联起来

  - (void)viewDidLoad

  {

  [superviewDidLoad];

  self.tableView.dataSource=self;

  }

  UITableViewDataSource的可选方法

  // 如果想改变tableView有几组要实现下面这个方法

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 默认只有1组

  // 第section组显示什么标题

  - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section;

  // 第section组显示什么尾部标题

  - (NSString *)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section;

  // 局部刷新表格 reloadRowsAtIndexPaths

  NSIndexPath *path = [NSIndexPathindexPathForRow:row inSection:0];

  [self.tableViewreloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];

  // 重新加载数据 用这个方法可实现加载更多数据

  [self.tableViewreloadData];

  // 调用上面这个方法的时候 会调用

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  方法进行重新赋值

  // 返回右边索引条显示的字符串数据

  - (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView;

  UITableView的代理协议 UITableViewDelegate

  通常处理tableView的事件都在代理协议中实现要使用UITableViewDelegate

  需要在加载的时候设置

  - (void)viewDidLoad

  {

  self.tableView.delegate =self;

  }

  // 设置每一行的高度不同

  - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath;

  // 当某一行被选中的时候

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

  {

  // 获取第几行被选中

  NSInteger *rowx = indexPath.row;

  }

  // 当表格开始拖拽的时候触发

  - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

  // 每一组头部显示的视图

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

  UITableView常用属性

  // 在表格头部增加一个控件

  self.tableView.tableHeaderView = [UIButtonbuttonWithType:UIButtonTypeContactAdd];

  // 在表格尾部增加一个控件

  self.tableView.tableFooterView = [[UISwitchalloc] init];

  // 设置表格统一行高

  self.tableView.rowHeight = 60;

  // 设置每组头的行高

  self.tableView.sectionHeaderHeight = 44;

  // 设置表格背景视图

  self.tableView.backgroundView =nil;

  // 设置表格背景颜色

  self.tableView.backgroundColor = [UIColorcolorWithRed:0.9 green:0.9blue:0.9 alpha:1];

  // 设置表格不允许被选中

  self.tableView.allowsSelection =NO;

  UITableView优化

  需要优化的方法

  // 每当有一个cell进入视野范围内,就会调用此方法, 当cell不可见的时候会进行自动释放 因此在屏幕中始终只有一个cell 在变化 所以我们要对这个cell的内存进行重用 实现如下

  - (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath

  {

  staticNSString *ID = @"hero";

  // 1.通过一个标识去缓存池中寻找可循环利用的cell

  // dequeue :出列 (查找)

  UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

  // 2.如果没有可循环利用的cell

  if (cell ==nil){

  cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];

  // NSLog(@"------缓存池找不到cell--%d",indexPath.row);

  }

  // 3.给cell设置新的数据

  // 取出模型

  Hero *hero =self.heros[indexPath.row];

  // 设置cell的数据

  cell.textLabel.text = hero.name;

  cell.detailTextLabel.text = hero.intro;

  cell.imageView.image = [UIImageimageNamed:hero.icon];

  return cell;

  }

  2.使用默认的UItableViewCell会非常影响性能,奇怪的是使用自定义的View,而非预定义的UItableViewCell,会快一些

  UITableView静态的Cell

  默认我们从 设计界面 拖进来的是动态的Cell

  静态的Cell 可以直接在cell上面进行拖动控件进行自定义cell


上一篇:详解object中的self和super
下一篇:IOS开发初学者需知的小技术

相关资讯

  • [2014-05-14 17:54:44] IOS开发初学者需知的小技术
  • [2014-04-21 13:52:51] IOS表格UITableView学习内容详解
  • [2014-04-08 09:16:00] 详解object中的self和super
  • [2014-03-31 13:19:41] IOS隐藏键盘代码的方法
  • [2013-08-05 12:06:45] 果粉给iOS 6的10个建议
  • [2013-08-02 15:58:06] iOS开发中防止键盘挡住UITextField解决方案
  • 【2013-2月28日】
    Java 软件工程师就业班
    >>热招中!


    【2013-2月28日】
    3G-Android 工程师就业班
    >>热招中!


    【2013-2月28日】
    3G-Android 工程师周末班
    >>热招中!

    【2013-2月28日】
    软件测试 工程师就业班
    >>热招中!