原始cell重用
//这里是根据 Identifier身份ID 去cell队列里寻找cell,这里的Identifier注意是非常重要的,相同Identifier的cell会放入同一个cell队列中,这句只有在cell数超过tableview能显示的最大值时才会走。也就是能重用的时候再走。也可以理解为队列里只保持隐藏着的cell,显示中的cell是不保存的。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
if (!cell) {//这里判断Identifier所属队列里有没有cell,没有就创建cell,有就跳过不创建。这也是复用的关键步骤,这里会先创建tableview能显示的最大cell数。
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];//这里就是创建cell并且绑定Identifier,同时放入相应队列中去。
}
NSDictionary *dic =searchResultArr[indexPath.row];
cell.textLabel.text = dic[@"storeName"];
return cell;//返回cell,显示出来。
手写自定义cell重用
static NSString *r = @"test";
// HomeOneTableViewCell是自定义的样式
HomeOneTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:r];
if (!cell)
{
cell = [[HomeOneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:r];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
Nib cell重用(需在xib上设置 Restoration ID)
OngoingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OngoingTableViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"OngoingTableViewCell" owner:nil options:nil]lastObject];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;