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不会出现的问题。

你可能感兴趣的:(iOS tableView reload时抖动问题处理)