对
UITableView
之Cell
和UITableViewHeaderFooterView
高度自适应并缓存,同时完美支持Storyboard
、Xib
、Masonry
,及注册方式
或者alloc方式
创建Cell
此库代码参考了FDTemplateLayoutCell在此感谢。
由于最近需要做一个类似微信聊天窗口的功能,牵涉到
UITableViewCell
的高度自适应,同时需要准确获取UITableView
的contentSize
并做相应的滚动和动画,在反复尝试使用系统自适应和FDTemplateLayoutCell均有一些小问题待处理,代码计算又太麻烦,故尝试自己实现。
- 系统自动计算无法满足准确获取
contentSize
。 - 系统自动计算效率较低(来回反复计算)导致滚动指示器跳动。
- 系统自动计算对UITableViewHeaderFooterView的自适应不太友好。
- FDTemplateLayoutCell似乎在
iOS10.2.1
会崩溃,而且好像有一些小问题。 - 代码计算太繁琐而且容易错,你喜欢就用吧。
- 此库可能会额外增加一个View提供布局需要(ps:如果自定义了分割线其实是不需要增加的)
注意:
BMTemplateLayoutCell的实现原理是:内部创建Cell,然后使用Block让外部进行设值和布局,内部在使用 layoutIfNeeded 强制布局,在获取Cell的contentView的最底部的View的最大高度为Cell需要的高度,所以使用者不可对您自定义的Cell设置任何的底部约束限制,所以可能需要您额外增加一个View(记住把它的高度最好设置为0,它的唯一功能是提供布局需要),直接由顶部布局下来即可,放心内部会处理好的
在
Cell
中必须保证有一个视图的最大Y
即是Cell
的高度,具体可以查看demo
中的xib
约束设置。
- 支持
xib
masonry
- 支持
注册方式
和alloc方式
创建Cell
@property (assign, nonatomic, getter=isScreenRotating) IBInspectable BOOL screenRotating; ///< 是否支持屏幕旋转,默认NO
- 部分代码暂未优化,以后会优化。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BMModel *model = self.dataArray[indexPath.row];
return [tableView bm_heightForCellWithCellClass:BMCell.class cacheByKey:model.ID configuration:^(__kindof BMCell *cell) {
cell.model = model;
}];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
BMModel *model = self.dataArray[section];
return [tableView bm_heightForHeaderFooterViewWithWithHeaderFooterViewClass:BMXibHeader.class cacheByKey:model.ID configuration:^(__kindof BMXibHeader *headerFooterView) {
headerFooterView.model = model;
}];
}
- CocoaPods
- 手动安装
- 2.0.0