解决UITableView刷新时跳动上移

2017-10-11  本文已影响0人  风_iOSer

昨天在项目中发现一个问题,一个列表滚动到底部时,刷新列表,会向上弹一下,百思不得其解,经过一番查询,原因是刷新列表时,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;
}
上一篇 下一篇

猜你喜欢

热点阅读