tableViewCell创建,复用,禁止复用总结

创建方式汇总,注册和不注册

Cell注册的两种方式
1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull NSString *)
2.tableView registerClass:(nullable Class) forCellReuseIdentifier:(nonnull NSString *)

Cell注册的形式:

(1)系统cell
    1.注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    2.不注册
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

(2)自定义cell
    1.注册
    [self.tableView registerClass:[xxxxCell class] forCellReuseIdentifier:@"cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

  2.不注册
    xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell==nil) {
      cell=[[xxxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

(3)自定义cellXib注册
    1.注册(可以复用)
    [tableView registerNib:[UINib nibWithNibName:@"xxxxViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    2.不注册(不会复用,每一次都是重新创建)
     xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@“xxxxCell" owner:self options:nil]lastObject];
    }

复用机制不多做赘述,只讲解一下注册的复用机制

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: identifier] ;

这个方法的作用是,当我们从重用队列中取cell的时候,如果没有,系统会帮我们创建我们给定类型的cell,如果有,则直接重用. 这种方式cell的样式为系统默认样式.在设置cell的方法中只需要:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 重用队列中取单元格 由于上面已经注册过单元格,系统会帮我们做判断,不用再次手动判断单元格是否存在

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier forIndexPath:indexPath] ;

    return cell ;
}

现在记录一种禁用复用的场景,淘宝详情页面相关的场景,在不同的cell中创建不同的视图,而且不实用复用机制,让每一个cell保存自己当前的页面.
只讲通过xib创建的视图
使用方法,采用注册xib做法
注册四种cell

[tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailTopViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailTopViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailMidViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailMidViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailBottomViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailBottomViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailBottomTableViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailBottomTableViewCellID];
if (section==0) {
        TopViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }else if(section==1){
        MidViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }else if(section==2){
        BottomViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }

创建cell的时候分别创建,因为是通过注册创建,当没有cell的时候系统自动帮你创建,并且放到复用池当中,所以你对每行的cell做一次操作之后,cell会记录所有的操作.当滑出屏幕的时候,系统放到复用池当中(注意,cell已经完全保留相关的操作),当再次出现到屏幕上时,会从复用池中获取对应的cell,其中的相关信息保存完全,不用重新加载.
这样就保证了每一个cell上的视图保存了相关操作的内容,当时在视图内嵌套的tableView刷新之类的需要自己重新手动刷新一次,否则不会重新赋值会导致不能展示视图.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,136评论 1 32
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,553评论 8 265
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,003评论 3 119
  • 这次参加二阶学习,第一天第二天没有愤怒的情绪,我都在怀疑陈浪老师这个方法管不管用。到了第三天的动态静心,呼吸这个环...
    郭福顺阅读 607评论 0 5
  • 考四六级,不知道从什么时候开始变成了大学学习英语的一个唯一目的。 看见学英语的,都会忍不住问问“你还要考六级吗,不...
    元气小老虎修炼基地阅读 162评论 0 0