iOS:UITableView 使用(二)--UITableViewCell复用/重用探究

    复用方式

// 方式1
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        ……
// 方式二(xib):    
        BQxxCell *cell = [tableView dequeueReusableCellWithIdentifier:kxx];
        if (nil == cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"BQFreightTableViewCell" owner:self options:nil]lastObject];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [tableView registerNib:[UINib nibWithNibName:@"BQxxCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kxx];           }
        ……
// 方式三
  1.XXTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"kxx"];
    if (nil == cell) {
        cell= [[[NSBundle mainBundle] loadNibNamed:@"XXTableViewCell" owner:nil options:nil] lastObject];
    }
    return cell;
  2.在xib中identifier属性必须写上kxx ,对应代码中的。
 
 
// 方式四
   1.之前register cell
   2.方法- dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
         每一种复用方式在创建另一个新的cell时候略有区别

       测试例子

         下面HERE处不同的复用方式, 然后滑动tableview,查看输出。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    static int times = 0;
    XXTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"kxx"];
    if (nil == cell) {
        // HERE
        times ++;
        NSLog(@"time :%d ",times);
    }
    return cell;
}
  •  (一)如果HERE是方式一,输出:time:1,time:2,time:3,time:4,time:5
  •  (二)如果HERE是方式二,输出:time:1
  •  (三)如果HERE是方式三,输出:time:1,time:2,time:3,time:4,time:5
  •  (四)如果HERE是方式四,输出:空
  • 其他如果不用复用方式,输出:time:1,time:2,time:3,time:4,time:5,time:6,time:7,time:8,……
   // 不使用复用方式
    XXTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"kxx"];
    if (nil == cell) {
        cell= [[[NSBundle mainBundle] loadNibNamed:@"XXTableViewCell" owner:nil options:nil] lastObject];
        NSLog(@"time :%d ",times);
    }
    return cell;    

      总结

  • dequeueReusableCellWithIdentifier方法有俩个作用,可以找到该identify已存在的“inVisible cell”,还有一个就是如果该identify被一个xib注册了,通过dequeueReusableCellWithIdentifier方法,可以获取到一个创建好的cell(可能注册这个动作,将一个xib文件”解压“到了内存,并关联一个identifier)。
  • 给一个cell的identifier赋值的方式有:initWith..identifier + xib中attributes inspector赋值 + register cell;
    验证上述说法:修改上述例子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    static int times = 0;
    XXTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"kxx"];
    if (nil == cell) {
        // HERE
        times ++;
        NSLog(@"time :%d ",times);
    }
    NSLog(@"cellForRow_before:%@",cell.textLabel.text);
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    NSLog(@"cellForRow_after:%@",cell.textLabel.text);
    return cell;
}
          如果是方式二,则输出:
time:1
cellForRow_before:(null)
cellForRow_after:0
cellForRow_before:(null)
cellForRow_after:1
cellForRow_before:(null)
cellForRow_after:2
cellForRow_before:(null)
cellForRow_after:3
cellForRow_before:(null)
cellForRow_after:4
cellForRow_before:1
cellForRow_after:5
cellForRow_before:2
cellForRow_after:6
         如果是方式1,则输出:
time:1
cellForRow_before:(null)
cellForRow_after:0
time:2
cellForRow_before:(null)
cellForRow_after:1
time:3
cellForRow_before:(null)
cellForRow_after:2
time:4
cellForRow_before:(null)
cellForRow_after:3
time:5
cellForRow_before:(null)
cellForRow_after:4
cellForRow_before:0
cellForRow_after:5
cellForRow_before:1
cellForRow_after:6
  注意:
1.方式二中,可复用的cell的text从1开始,从xib中创建的第一个cell,它的identify还没有注册,所有identify值没有,不能复用
2:如果在HERE这里有以一些对cell的属性赋值,或者调用一些方法,注意cell的复用方式,有可能是所有cell起作用了,也有可能只有一个cell起作用了,或者都没起作用。
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值