自定义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];

你可能感兴趣的:(自定义UITableViewCell(registerNib: 与 registerClass: 的区别))