iOS tableview 刷新时跳动错位

2018/2/12
解决tableview使用estimatedRowHeight方式自动布局时,刷新reloadData会出现跳动错位的问题,
原因可能是因为在刷新的时候,cell的布局在垂直方向可能不清晰,导致cell的高度布局计算错误,所以可以先缓存一份之前的高度

{
    _tableView.estimatedRowHeight = 100;
    _tableView.rowHeight = UITableViewAutomaticDimension;
}
- (NSMutableDictionary *)cellHightDict {
    if (!_cellHightDict) {
        _cellHightDict = [NSMutableDictionary new];
    }
    return _cellHightDict;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [self.viewModel.dataSource safeObjectAtIndex:indexPath.row];
    NSString *cellName = [dic objectForKey:@"cellName"];
    [self.cellHightDict setObject:@(cell.frame.size.height) forKey:[NSString stringWithFormat:@"%@%ld",cellName, (long)indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [self.viewModel.dataSource safeObjectAtIndex:indexPath.row];
    NSString *cellName = [dic objectForKey:@"cellName"];
    CGFloat height = [[self.cellHightDict objectForKey:[NSString stringWithFormat:@"%@%ld",cellName,  (long)indexPath.row]] floatValue];
    if (height == 0) {
        return 100;
    }
    return height;
}

你可能感兴趣的:(iOS tableview 刷新时跳动错位)