自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

1.自定义cell时,

若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib

registerNib注册: [_tableView registerNib:[UINib nibWithNibName:@"xxxxxCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];

不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

registerClass注册: [_tableView registerClass:[xxxxxCell class] forCellReuseIdentifier:kCellIdentify];

2.需不需要注册?

使用dequeueReusableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

使用dequeueReusableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

xxxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId forIndexPath:indexPath];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容