demo:https://github.com/ITHanYong/AutoTableViewCell.git
#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
————————————————
版权声明:本文为CSDN博主「零粹」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_15289761/article/details/106897250
网友评论