demo:https://github.com/ITHanYong/AutoTableViewCell.gitgit
#import "BaseViewController.h" @interface BaseViewController () @property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度 @end @implementation BaseViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath]; if(height){ return height.floatValue; }else{ return 100; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { NSNumber *height = @(cell.frame.size.height); [self.heightAtIndexPath setObject:height forKey:indexPath]; } #pragma mark - Getters - (NSMutableDictionary *)heightAtIndexPath { if (!_heightAtIndexPath) { _heightAtIndexPath = [NSMutableDictionary dictionary]; } return _heightAtIndexPath; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end