关于UITableViewCell的重用

来源:互联网 发布:网络上jr是什么意思 编辑:程序博客网 时间:2024/05/21 17:51

整理印象笔记之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];    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;}
原创粉丝点击