UITableViewCell重用相关

1、cell重用方法

dequeueReusableCellWithIdentifier:(推荐使用)
dequeueReusableCellWithIdentifier:indexPath:(iOS6后新方法,获取的cell类型必须是事先注册过才行,这也是新方法与前面方法的区别所在,也因此个人不推荐使用)

2、cell注册方法

(注册说白了,就是告诉tableview在缓存池没有可重用cell的时候创建什么类型的cell)

通过代码注册:

    [self.myTableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]
    [self.myTableView registerNib:<#(nullable UINib *)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]

通过xib或者storyboard注册:

直接给与tableview属性连线的xib或者stroyboard当中cell设置重用标识符即完成注册

3、数据源方法

(UITableViewDataSource)

不需要注册cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //从缓存池当中获取可重用的cell
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   
   if (!cell)//没有可以重用的cell,旧需要手动创建,并加入到缓存池当中
   {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }
   
   return cell;
}

必须事先注册cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //从缓存池当中获取可重用的cell,如果没有救创建标识符尾cell的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    return cell;
}

4、注意事项

如果采用重用机制,需要每次重新覆盖cell数据,
(通常更新模型,并重新给cell的模型属性赋值)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容