UITableViewCell(registerNib: 与 registerClass: )

参考http://blog.csdn.net/youngsblog/article/details/44536143,http://www.cocoachina.com/bbs/read.php?tid=290160

一、使用xib###

使用xib分为两种方式

1、只有xib


//返回每行显示的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//1 创建可重用的cell

static NSString *reuseId = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

if (cell == nil) {

//从xib中加载cell

//cell = [[[NSBundle mainBundle] loadNibNamed:@"CZGroupBuyingCell" owner:nil options:nil] lastObject];

//从xib中加载view的另一种方式

cell= [[[UINib nibWithNibName:@"CZGroupBuyingCell" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];

}

//2 设置cell内部的子控件

//2.1 获取当前要展示的数据

CZGroupBuying *gb = self.groupBuyings[indexPath.row];

//2.2 设置值

UILabel *titleView = (UILabel *)[cell viewWithTag:10];  //因为只有xib 所以用的以前比较老的方式通过tag来找对应的控件

titleView.text = gb.title;

//3 返回

return cell;

}

2、xib中的cell指定class为自定义的class(注意不是设置File's Owner的class) 两种注册方式

UITableViewCell(registerNib: 与 registerClass: )_第1张图片
202133232.png

第一种:

[self.tableView registerClass:[XMGDealCell class] forCellReuseIdentifier:@"cell"];
还需在Cell的.m文件中实现:initWithStyle并自己加载Nib:
 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
    if(self) { 
        self =[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CZGroupBuyingCell class]) owner:nil options:nil] lastObject];
        [self initCell];  
    }  
    return self;  

第二组种:

[self.tableView registerNib:[UINib nibWithNibName:@"CZGroupBuyingCell" bundle:nil] forCellReuseIdentifier:ComplaintHandingCellIdentifier];  

二、不使用xib(纯代码)###

[_self.tableView registerClass:[CZGroupBuyingCell class] forCellReuseIdentifier:@"cell"];  

另外要注意的:###

1、dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别:
前者不必向tableView注册cell的Identifier,但需要判断获取的cell是否为nil;
后者则必须向table注册cell,可省略判断获取的cell是否为空,因为无可复用cell时runtime将使用注册时提供的资源去新建一个cell并返回

2、自定义cell时,记得将其他内容加到self.contentView 上,而不是直接添加到 cell 本身上

总结:
1.自定义cell时,
若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2.需不需要注册?
使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

3、dequeueReuseableCellWithIdentifier:获取cell时,Cell一般写法

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

4、为了将判断cell为空的逻辑写在cell里,给外面留下整洁简易逻辑,封装强迫症者比如我下可以稍微改下(比如我自定义的cell名称是XMGDealCell)

//1.留给外层调用的是一个静态方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
    // 取出模型数据
    cell.deal = self.deals[indexPath.row];
    
    return cell;
}

在Cell里的cellWithTableView:方法

//2.自定义静态方法主要逻辑是到缓存里面取出cell,判断cell是否为空并创建
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cell";
    // 创建cell
    XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[XMGDealCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

//3.在initWithStyle:reuseIdentifier:方法中添加子控件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
if (self) 
{ 
// cell页面布局 
[self setupView]; 
} 
return self; 
} 

// 3.重写模型的set方法
- (void)setDeal:(XMGDeal *)deal
{
    _deal = deal;
    // 设置数据
    self.iconView.image = [UIImage imageNamed:deal.icon];
    self.titleLabel.text = deal.title;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
}

UITableViewCell(registerNib: 与 registerClass: )_第2张图片
A5C1910C-A680-468E-A97F-08027EAC3C30.png

你可能感兴趣的:(UITableViewCell(registerNib: 与 registerClass: ))