2018/2/12
解決tableview使用estimatedRowHeight方式自動(dòng)布局時(shí)读宙,刷新reloadData會(huì)出現(xiàn)跳動(dòng)錯(cuò)位的問(wèn)題绣硝,
原因可能是因?yàn)樵谒⑿碌臅r(shí)候拓提,cell的布局在垂直方向可能不清晰停蕉,導(dǎo)致cell的高度布局計(jì)算錯(cuò)誤勺三,所以可以先緩存一份之前的高度
{
_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;
}