iOS tableView reload时抖动问题处理

tableView的cell采用cell自适应高度,并且各个cell高度不同的情况下,在reload时,tableView可能会发生异常抖动。
原因就是因为没有实现返回高度的代理,tableView刷新渲染时,并不明确cell的高度具体会是多少,以及cell自适应高度的机制问题。可能的异常发生逻辑就是系统先拿模糊高度进行渲染,当cell高度确定后,又调整了高度,所以发生的抖动。

我的解决方式如下:
willDisplayCell时用字典缓存cell的高度-> estimatedHeightForRowAtIndexPath方法返回
用字典存储cell出现后的高度,刷新时让系统使用我们对于每个cell缓存的高度。

#pragma mark - 解决动态cell高度 reloadData刷新抖动的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
   _estimatedRowHeightCache[indexPath] = @(cell.frame.size.height);
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return [_estimatedRowHeightCache[indexPath] floatValue] ?:50; // 为0时最好返回最接近的预估高度
}

tip:

1. 一般情况下不是动态变化的界面字典的key值可以用indexPath,而如果cell位置会动态变化,或者隐藏显示后刷新界面,key值就不能使用indexPath, 高度缓存对应关系会是错的, 应该使用cell对应的界面类型/业务类型,否则依然会有抖动问题。

对于每个cell对应一个model的情况,我们可以将高度缓存至model中

#pragma mark - 解决动态cell高度 reloadData刷新抖动的问题

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     DemoModel *model = self.dataArray[indexPath.row];
     model.esHegiht = cell.frame.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     DemoModel *model = self.dataArray[indexPath.row];
     return model.esHegiht?:50; // 为0时最好返回最接近的预估高度
}

对于所有cell对应一个model的情况,比如一些详情页,我们可以以cell的类型缓存高度

/**
伪代码
*/
- (void)buildDataArray {
    // tableView数据源推荐使用这种方式 可以摆脱if indexPath.row的判断,方便维护。
    // 可以不使用枚举,使用字符串,cell的类名都可以,根据业务。
    self.dataArray = @[@(InfoCellType1),@(InfoCellType2)];
}

#pragma mark - 解决动态cell高度 reloadData刷新抖动的问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoCellType type = [self.dataArray[indexPath.row] integerValue];
    __weak typeof(self) weakSelf = self;
    DemoTableViewCell *cell;
    switch (type) {
        case InfoCellType1:
            cell = [tableView dequeueReusableCellWithIdentifier:@"cellId1" forIndexPath:indexPath];
            break;
        case InfoCellType2:{
            cell = [tableView dequeueReusableCellWithIdentifier:@"cellId2" forIndexPath:indexPath];
        }
            break;
        default:
            break;
    }
   cell.model = self.infoModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
 // 取出对应的界面类型
  NSNumber *typeNumber = self.dataArray[indexPath.row];
   _estimatedRowHeightCache[typeNumber] = @(cell.frame.size.height);
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSNumber *typeNumber = self.dataArray[indexPath.row];
    return [_estimatedRowHeightCache[typeNumber] floatValue] ?:50; // 为0时最好返回最接近的预估高度
}
2 .返回预估模糊高度不能返回0,否则会发生cell不会出现的问题。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,174评论 1 32
  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 1,568评论 2 7
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,407评论 0 1
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,333评论 1 23
  • 这篇文章可以让你轻松理解什么是AOP 先说一个Spring是什么吧,大家都是它是一个框架,但框架这个词对新手有点抽...
    琴匣自鸣阅读 310评论 0 0