UITableView的registerClass forCellReuseIdentifier释义

UITableViewCell的重用声明有两种方式:

1. registerClass方式。在初始化UITableView的时候顺便声明一下cell的重用,如下所示。

[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
这时就不需要在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中添加如下代码:

static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}//代码片段A

而只需要做如下声明:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
如果registerClass和代码片段A同时添加就会出现错误

2. cell == nil方式,该方式更常用,书写方式如代码片段A

static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

参考文章:http://blog.csdn.net/u010367539/article/details/38900843

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值