关于UITableViewCell的重用

整理印象笔记之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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值