UITableView-FDTemplateLayoutCell缓存cell高度时,cell重叠高度为0的问题解决

使用UITableView-FDTemplateLayoutCell框架缓存cell高度

1. UITableView-FDTemplateLayoutCell框架集成

使用cocoapods导入:pod 'UITableView+FDTemplateLayoutCell'


2. git源码链接:https://github.com/forkingdog/UITableView-FDTemplateLayoutCell


3.使用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"identifer" cacheByIndexPath:indexPath configuration:^(id cell) {
        // 配置 cell 的数据源,和 "cellForRow" 干的事一致,比如:
        cell.entity = self.feedEntities[indexPath.row];
    }];
}
或:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Entity *entity = self.entities[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier:@"identifer" cacheByKey:entity.uid configuration:^(id cell) {
        // configurations
    }];
}

使用UITableView-FDTemplateLayoutCell框架缓存cell高度的好处不再多说,下面说一下,可能遇到的问题

a. 运行崩溃报错 NSAssert(templateCell != nil, @"Cell must be registered to table view for identifier - %@", identifier);

原因:官方是这么说明的:

  • 每个 UITableViewCell ReuseID 一一对应的 template layout cell这个 cell 只为了参加高度计算,不会真的显示到屏幕上;它通过 UITableView 的 -dequeueCellForReuseIdentifier: 方法 lazy 创建并保存,所以要求这个 ReuseID 必须已经被注册到了 UITableView 中,也就是说,要么是 Storyboard 中的原型 cell,要么就是使用了 UITableView 的 -registerClass:forCellReuseIdentifier: 或 -registerNib:forCellReuseIdentifier:其中之一的注册方法。

解决:意思就是说你需要注册cell对应的identifier。

b.结合masonry 搭建页面时, 设为固定高度是没有问题的,但是使用框架缓存cell高度时,cell重叠所有cell高度都为0。

原因:计算cell高度时,因Y方向上约束不完善,无法确定cell的高度。

解决:设置约束时,由上到下设置,最后一个需要设置距底部边距。一定能让系统确定这个cell的高度



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值