xib 创建tableviewCell的重用

有好多人说用xib创建的cell不再遵循重用机制,对此很不理解,经验证,即使是用xib创建的cell也是遵循重用机制的。注意的是:用xib创建cell的时候Identifier设定的与cellForRowAtIndexPath方法中的一致。

//cell中拖放一个Label,上面的文字是Label

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

{

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custTabCell"];

    if (!cell) {

        cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil]lastObject];

    }

    //验证

    //如果cell是从重用池中取出来的,那么test上面的文字就不是Label了,也就不再改变会一直重复循环

    if ([cell.test.text isEqualToString:@"Label"] ) {

        NSInteger i = indexPath.row;

        cell.test.text = [NSString stringWithFormat:@"%ld", (long)i];

    }

    return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 40;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值