整理印象笔记之UITableViewCell的重用
第一种:
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//注册重用标识
static NSString *cellID = @"cell";
//缓存池查找可循环的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//没有则初始化(代码自定义的cell)
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
//如果是xib自定义的cell
/*
TTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([TTCell class]) owner:nil options:nil] lastObject];
}
*/
cell.textLabel.text = @"waaa";
return cell;
}
第二种:
//定义一个全局变量
static NSString *cellID = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
//注册标识对应的cell类型(代码自定义cell执行这里)
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
//如果是xib自定义的cell需要注册nib文件
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTCell class]) bundle:nil] forCellReuseIdentifier:cellID];
}
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//设置重用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.textLabel.text = @"tableViewCell";
return cell;
}
第三种:
在storyboard中设置tableView的Dynamic Prototypes Cell
设置cell的重用标识
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//注册重用标识
static NSString *cellID = @"cell";
//缓存池查找可循环的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
cell.textLabel.text = @"tableViewCell";
return cell;
}