tableView:cellForRowAtIndexPath:

通常在创建完UITableViewController后,会看到UITableViewDataSource的一个实现函数如下

复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell"];
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  //config the cell
  return cell;
}
复制代码

而这些代码又是什么意思呢?

每一个UITableView里都维护着一个cell队列,当UITableView刚加载的时候,cell队列里是没有任何数据的。dequeueResableCellWithIdentifier从字面上理解就是”出列可重用的cell",也就是根据一个标识identifier从cell队列里取出一个UITableViewCell,当然了,如果cell队列里没有此标识的cell,调用此方法的结果就是返回nil。因此,在UITableView刚加载的时候,cell队列里没有可用的cell,所以必须通过语句

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

来创建对应CellIdentifier标识的UITableViewCell实例。

而当UITableView在滚动的时候导致UITableViewCell滚出手机屏幕视图的时候,程序会将这一个UITalbeViewCell实例放入此UITableView所维护的cell队列中。当UITableview中有新的UITableViewCell需要展现在手机屏幕视图上时,就会调用tableView:cellForRowAtIndexPath:方法了,因此我们可以知道以下几点:

1-重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)。

2-这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂。

3-设计的关键是实现cell和数据的完全分离

如果不想重用UITableViewCell实例,如在一个每一行都显示不同内容的UITableView实例时,我们可以用如下的方法

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];

来重新定义标识。

这样每一行都有其对应的identifier,从cell队列里取出来只有两个结果:

1-cell队列里没有此identifier对应的UITableViewCell实例,返回nil

2-cell队列里有此identifier对应的UITableViewCell实例,而且不会有重用到其他不同行的cell的情况
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取UITableView的内容可以通过UITableView的dataSource属性来实现。dataSource属性是一个遵循UITableViewDataSource协议的对象,可以通过该协议方法来获取表格的数据源。 下面是一个示例代码,演示如何获取UITableView的内容: ``` // 获取UITableView的dataSource id<UITableViewDataSource> dataSource = tableView.dataSource; // 获取UITableView的section数 NSInteger numberOfSections = [dataSource numberOfSectionsInTableView:tableView]; // 遍历每个section,获取每个section中的row数以及每个row的内容 for (NSInteger section = 0; section < numberOfSections; section++) { // 获取每个section中的row数 NSInteger numberOfRows = [dataSource tableView:tableView numberOfRowsInSection:section]; // 遍历每个row,获取每个row的内容 for (NSInteger row = 0; row < numberOfRows; row++) { // 获取每个rowindexPath NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; // 获取每个row的内容 UITableViewCell *cell = [dataSource tableView:tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; NSLog(@"Section %ld, Row %ld: %@", section, row, cellText); } } ``` 在上述示例代码中,我们首先获取UITableView的dataSource,然后遍历每个section和row,获取每个row的内容。在获取每个row的内容时,我们使用了UITableViewDataSource协议中的tableView:cellForRowAtIndexPath:方法来获取每个row对应的UITableViewCell对象,然后从UITableViewCell对象中获取文本内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值