iOS 注册cell

2016.4.14

1.自定义cell时,

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

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

文/XiaXiang(作者)

原文链接:http://www.jianshu.com/p/66420a87b844

著作权归作者所有,转载请联系作者获得授权,并标注“作者”。

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

if(cell ==nil) {

cell = [[PreSallDetailCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cellid"];

}

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

xib:

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

xxxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

代码

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

xxxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

你可能感兴趣的:(iOS 注册cell)