主要實(shí)現(xiàn)是通過緩存高度來解決
/**
?* 緩存高度
?*/
@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;
}