iOS UITableViewCell 高度的缓存

关于如何让 tableViewCell的高度 给缓存起来

  • 1.在网上看了很多人写的 关于 UITableViewCell高度的缓存, 有的是用 数组, NSCache,模型中回调Cell方法,来存储Cell的高度. 可往往这么做牺牲的东西就太多了.

  • 2.网上大部分程序猿都希望的做法: 模型中有一个 cellHeight 简简单单的记录住当前cell的高度.

  • 3.我也是这样的做法,将cell的高度放在 当前数据的模型中,如果没有,则计算,如果有,则取出

实例代码(我是用 Masonry进行布局的)

(1)当前模型

/*下面三个属性是 打酱油的*/
@property (nonatomic,strong) UIColor *iconColor;
@property (nonatomic,copy  ) NSString *name;
@property (nonatomic,copy  ) NSString *job;

/* 内容(这里面文字 有多有少) */
@property (nonatomic,copy  ) NSString *content;
/* 来记录 cell 的高度 */
@property (nonatomic,assign) CGFloat  cellHeight;

(2)当前cell.h

@class FriendModel;
@interface FriendCell : UITableViewCell
/* 类方法 实现 cell的 循环利用 */
+ (instancetype)friendCellWithTableView:(UITableView *)tableView;
/* 通过 setter 方法, 给cell上的控件 设置值 */
@property (nonatomic,strong) FriendModel *friendM;

@end

当前cell.m

- (void)setFriendM:(FriendModel *)friendM{
    _friendM = friendM;
    
    // 给控件设置 内容
    _iconView.backgroundColor = friendM.iconColor;
    _nameLabel.text = friendM.name;
    _jobLabel.text = friendM.job;
    _contentLabel.text = friendM.content;
    
    /* 调用一次 layoutIfNeeded, 来重新约束控件 */ 
    [self layoutIfNeeded];
}
// 布局 控件 layoutSubviews
- (void)layoutSubviews{
    [super layoutSubviews];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(15);
        make.size.mas_equalTo(50);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(15);
        make.top.equalTo(_iconView.mas_top);
        make.height.mas_equalTo(20);
    }];
    
    [_jobLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.height.equalTo(_nameLabel);
        make.top.equalTo(_nameLabel.mas_bottom).offset(10);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_jobLabel.mas_bottom).offset(15);
    }];
    
    
    /* 这个地方 判断当前模型的 cellHeight 是否有值,如果没有,则获取高度 */
    if(_friendM.cellHeight == 0){
        [self layoutIfNeeded]; // 这里必须加, 因为这里是获取高度,相当于布局得刷新一遍
        _friendM.cellHeight = CGRectGetMaxY(_contentLabel.frame) + 20;
    }
}

(3)当前控制器

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendModel *friendM = self.dataArr[indexPath.row];
    return friendM.cellHeight; // 直接返回当前模型的cellHeight就OK
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • UITableViewCell 父类是UIView UITableView的每一行都是一个UITableViewC...
    翻这个墙阅读 11,690评论 0 1
  • 前言 由于最近两个多月,笔者正和小伙伴们忙于对公司新项目的开发,笔者主要负责项目整体架构的搭建以及功能模块的分工。...
    CoderMikeHe阅读 27,322评论 74 270
  • 我昨晚梦见了你 螓首蛾眉眉如柳 画眉如黛黛如歌 柳眉桃眼不胜春 温柔被你唱成了歌 融化了星河
    良乆囍阅读 1,778评论 0 2
  • 来生想做哪国人? 不一定值得一游的花园 开卷有益健康 读万卷书再行万里路 孤独的绿洲
    lcl861818阅读 2,515评论 0 0
  • 最近公司附近开了个西餐厅,味道不错,吃过几次跟店长熟了起来,才知道这家跟附近一些餐饮店的老板都是同一个,不明觉厉。...
    几米一回头阅读 12,332评论 2 9

友情链接更多精彩内容