iOS dequeueReusableCellWithIdentifier: forIndex: 与dequeueReusableCellWithIdentif比较

(1)dequeueReusableCellWithIdentifier: forIndex: 方法是IOS6.0+版本才出现的,新的重用机制方法;只能在IOS 6.0以上系统才有效;
注意官方SDK说明: newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
即:使用dequeueReusableCellWithIdentifier: forIndex: 必须使用配套的

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier 

或者

 - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
 ```
 - 这个方法。对每一个tableview的cell进行注册。

1,使用dequeueReusableCellWithIdentifier:CELL_INDENTIFIER  后面cell需要判断为nil时候,生成一个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 static NSString* CELL_INDENTIFIER=@"IPHONE_LIST_CELL";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: CELL_INDENTIFIER];
 ``` 
//cell最初从cell队列中获取,为nil。并不存在

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CELL_INDENTIFIER];

      //设置你的cell内容

       cell.textLabel.text=[listArray objectAtIndex:indexPath.row];

}

   return cell;
}

2,用新方法的写法:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell" forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_INDENTIFIER forIndexPath:indexPath];
//cell 从队列中获取得到,不会为nil  原因就是在初始化tablview的时候已经用到


[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"IPHONE_LIST_CELL"];进行了注册。

cell.textLabel.text=[listArray objectAtIndex:indexPath.row];


return cell;

}

注明:使用2的新方式重用机制,如果没有对tableview
registerClass注册cell的话,会闪退报错:
must register a nib or a class for the identifier or connect a prototype cell in a storyboard’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值