复用方式
// 方式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起作用了,或者都没起作用。
2:如果在HERE这里有以一些对cell的属性赋值,或者调用一些方法,注意cell的复用方式,有可能是所有cell起作用了,也有可能只有一个cell起作用了,或者都没起作用。