tableView 刷新 抖动解决办法

tableView 刷新 抖动解决办法

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
  }

这个方法是给tableview默认加一个预估的cell值,在iOS11以下可以使用这个方法。里面返回UITableViewAutomaticDimension,如果在iOS11上出了reload闪屏,在创建tableview的时候使用

if(@avalible(IOS,*)){
  tableview.estimatedRowHeight = 0
}
or   
  _tableView.estimatedSectionHeaderHeight = 0;
  _tableView.estimatedSectionFooterHeight = 0;
  _tableView.estimatedRowHeight = 0;

就行了产生的原因是在创建tablecell的时候系统给加了一个默认预估的cell高度,每次reload都用这个高度计算cell,禁用或者设为0就行了


知识拓展:

  1. estimatedHeightForRowAtIndexPath:
  • 在没有实现estimatedHeightForRowAtIndexPath方法时,这三个方法(numberOfRowsInSection,heightForRowAtIndexPath,cellForRowAtIndexPath)的调用顺序如下:

    1.先调用numberOfRowsInSection
    2.再调用heightForRowAtIndexPath
    3.再调用cellForRowAtIndexPath
    
  • 实现了estimatedHeightForRowAtIndexPath方法后,调用顺序是这样的

    1.numberOfRowsInSection
    2.estimatedHeightForRowAtIndexPath
    3.cellForRowAtIndexPath
    4.heightForRowAtIndexPath
    
  • 接下来我来说明以下出现EXC_BAD_ACCESS错误的原因:

    当你没用实现estimatedHeightForRowAtIndexPath方法时,
    heightForRowAtIndexPath方法获取cell的时候,
    会形成一个死循环,那就是numberOfRowsInSection和heightForRowAtIndexPath方法不断互调,
    最后程序崩溃.
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,138评论 1 32
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,674评论 3 3
  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 1,533评论 2 7
  • 目录 UITableView 复用原理 UITableViewDataSource&&UITableViewDel...
    kirito_song阅读 3,269评论 0 11
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,088评论 3 38