美文网首页
自定义UITableViewCell(registerNib:

自定义UITableViewCell(registerNib:

作者: Bonew01 | 来源:发表于2022-08-10 14:20 被阅读0次

    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:

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