UITableViewCell复用的一些问题总结


这里总结一下tableViewCell复用的一些坑,以免再跳进去. @!@


一般来说,复用cell只是复用的控件模板,并不是数据,但有时情况不一样,比如按钮点击,选中或者勾选,对cell或者控件做一些状态的修改,这时候就很容易出现复用的问题,注意观察的话,一划动tableView到二屏开始就会发现有些cell长得和第一屏的某些cell一模一样,或者再往回拉,发现之前的cell上布满重影,很乱,感觉像是车祸现场......
解决的思路:
1. 一般对于这种改变状态的,都是需要记录状态的,改变之后放到在数据源里,刷新UI控件.
2. 实际开发中都是通过网络请求获取到数据,json转换成Model,然后在model里实现改变状态的逻辑,在控件里赋值时根据不同的状态,更新UI.

创建cell的方法:

  • 1 XIB注册和纯代码注册
  //XIB注册
 [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([VideoCommentTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"VideoCommentTableViewCell"];

//纯代码注册
 [self.tableView registerClass:[VideoCommentTableViewCell class] forCellReuseIdentifier:@"VideoCommentTableViewCell"];
  
//StoryBoard的话不用注册 在面板上打上reuseID就行,只要创建cell的代理方法里的reuseID一致就行

注册cell的实现方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCommentTableViewCell"];
    cell.commentModel = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
    1. 有些人觉得注册分得太开,麻烦! so =>>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID=@"VideoCommentTableViewCell";
    VideoCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[VideoCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.commentModel = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
    1. 其实还可以对cell进行一层封装,调用工厂方法就行,必传参数只要传递一个数据源和tableView对象,其他参数任意根据业务需求来传.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MyListCell setupCellWithTableView:tableView data:[self.dataSource ObjectAtIndex:indexPath.row] ];
}

暴力解决复用问题, 没有什么是暴力解决不了的,如果有那就是还不够暴力

// 从队列里面复用时调用(我用过一次,个别控件不需要复用的,但是老是发生复用却找不到原因,无奈之下直接remove让其停止复用)
- (void)prepareForReuse {
    [super prepareForReuse];
    [_videoView reset];
}


//还有网上看到的,不知道有木有用,反正我是不想用这种
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID=@"VideoCommentTableViewCell";
    VideoCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[VideoCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.commentModel = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
     for (UIView* view  in  cell.contentView.subviews){       
          [view  removeFromSuperview];
      }
      
   }
    return cell;
}

参看Demo源码

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

推荐阅读更多精彩内容

  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 1,650评论 1 4
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • 越来越喜《传习录》学习,虽很艰苦,但每一天,都觉得心上功夫在增长。 先来两剂先生良药: “日间工夫觉纷扰,则静坐,...
    轻声慢语阅读 862评论 1 5
  • 今天终于克服懒惰,送完儿子上学,开始到方洲公园跑步,跑了一圈就跑不动,就转为快走,一圈圈走,记录感慨,公园里老...
    涅般木阅读 204评论 0 2