tableView

作者: CoderJackieYip | 来源:发表于2016-05-13 13:48 被阅读16次
  • 设置dataSource将自动调用其数据源方法。(storyboard\xib\code里设置情况一致)
  • 使用registerNib 或 registerClass注册Cell
  - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
  - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier;
// 使用方式:创建tableView后,用tableView调用,如下:
static NSString *cellIdentifier = @"customCell";
[tableView registerClass:[CustomCell class] forCellReuseIdentifier: cellIdentifier];

注册cell后,使用如下方法创建cell(6.0启用)

static NSString *cellIdentifier = @"customCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath:indexPath];

没有注册cell, 只能使用如下方法创建cell(更加繁琐,5.0的API)

static NSString *cellIdentifier = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cennIdentifier];
if  (cell = nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

总结:

  • 自定义cell时,若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的-(void)awakeFromNib
  • 不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

  • 使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
  • 使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

相关文章

网友评论

      本文标题:tableView

      本文链接:https://www.haomeiwen.com/subject/ysxmrttx.html