美文网首页iOS常用
iOS:自动适配tableViewCell的高度与缓存其高度,避

iOS:自动适配tableViewCell的高度与缓存其高度,避

作者: 缘來諟夢 | 来源:发表于2021-10-13 17:22 被阅读0次

    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

    相关文章

      网友评论

        本文标题:iOS:自动适配tableViewCell的高度与缓存其高度,避

        本文链接:https://www.haomeiwen.com/subject/fnkeoltx.html