主要实现是通过缓存高度来解决
/**
* 缓存高度
*/
@property (nonatomic,strong) NSMutableDictionary *cellHeightsDictionary;
//初始化
self.tableView.estimatedRowHeight=50;
self.cellHeightsDictionary=[[NSMutableDictionary alloc]init];
// 保存高度
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
}
// 获取高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
if (height) return height.doubleValue;
return UITableViewAutomaticDimension;
}