有好多人说用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;
}