美文网首页
iOS estimatedRowHeight与UITableVi

iOS estimatedRowHeight与UITableVi

作者: 明似水 | 来源:发表于2019-10-29 16:16 被阅读0次

解决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 {
    [self.cellHightDict setObject:@(cell.frame.size.height) forKey:[NSString stringWithFormat:@"%ld%ld",(long)indexPath.row, (long)indexPath.row]];
}

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

END.

相关文章

网友评论

      本文标题:iOS estimatedRowHeight与UITableVi

      本文链接:https://www.haomeiwen.com/subject/iqdgvctx.html