使用UITableView-FDTemplateLayoutCell框架缓存cell高度
1. UITableView-FDTemplateLayoutCell框架集成
使用cocoapods导入:pod '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的高度