解决UITableView刷新时跳动上移

昨天在项目中发现一个问题,一个列表滚动到底部时,刷新列表,会向上弹一下,百思不得其解,经过一番查询,原因是刷新列表时,tableView会重新计算高度,得到用缓存行高的办法如下:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{


  NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
  NSNumber *heigt = self.cellheightDic[key];
  if (heigt == nil) {
      heigt = @(100);
  }

    return heigt.floatValue;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

  NSLog(@"heig = %ld",indexPath.row);

  NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
  NSNumber *heigt = self.cellheightDic[key];
  if (heigt == nil) {
    heigt = @(self.cell.cellHeight);
    [self.cellheightDic setValue:heigt forKey:key];
    }
return heigt.floatValue;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,305评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,663评论 7 249
  • 有一个工程项目结束两年了,剩下26台只用了一年多的空调还未处理。原价值过5万元,有二手商过来收购,只愿出价...
    玉扳手阅读 212评论 0 0
  • UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下...
    请叫我魔法师阅读 538评论 0 0
  • 回忆过去一瞬间眼泪涌上眼角, 其实谁对谁以后都没有把握,毕竟不是谈恋爱,你要有一个方面特别吸引人,可以是幽默,帅气...
    细节阅读 229评论 0 0