Cell自定义registerNib:与registerClass:

 一、使用Nib
registerNib:  forCellReuseIdentifier:方法向数据源注册cell
[self.table registerNib:[UINib nibWithNibName:@"xxCell" bundle:nil] forCellReuseIdentifier:CellIdentify];
在cellForRowAtIndexPath中使用dequeueReuseableCellWithIdentifier:  forIndexPath:获取重用的cell,若无重用的cell,将自动使用所提供的nib文件创建cell并返回
xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify forIndexPath:indexPath];  
获取cell时若无可重用cell,将创建新的cell并调用其中的 awakeFromNib 方法,可通过重写这个方法进行页面布局
二、不使用Nib 

registerClass:  forCellReuseIdentifier:方法注册 

[self.table registerClass:[xxCell class] forCellReuseIdentifier:CellIdentify]; 

调用重写自定义cell的initWithStyle: withReuseableCellIdentifier:方法进行布局 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
{  
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
    if (self)  
    {    
        [self setSubView]; //cell页面UI
    }  
    return self;  
}  

在cellForRowAtIndexPath中使用dequeueReuseableCellWithIdentifier:  forIndexPath:获取重用的cell,若无重用的cell,将自动使用所提供的class类创建cell并返回 

xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify forIndexPath:indexPath];
获取cell时若无可重用cell,将调用cell中的 initWithStyle:  withReuseableCellIdentifier:方法创建新的cell

三、总结如下:
1、自定义cell
若使用Nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用Nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2、自定义的cell需不需要注册
使用dequeueReuseableCellWithIdentifier: 无需注册,但是必须对cell进行判空,若空则创建新的cell;
使用dequeueReuseableCellWithIdentifier:  forIndexPath: 必须注册,返回的cell可省略空值判断
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值