对UITableView的Cell和UITableViewHeaderFooterView进行高度自适应并缓存,同时完美支持Storyboard、Xib、Masonry,以及通过注册方式或alloc方式创建Cell
此库的代码参考了FDTemplateLayoutCell,在此表示感谢。
由于最近需要做一个类似微信聊天窗口的功能,涉及到UITableViewCell的高度自适应,同时需要准确获取UITableView的contentSize并进行相应的滚动和动画,在反复尝试使用系统自适应和FDTemplateLayoutCell后均有一些小问题待处理,代码计算又太麻烦,故尝试自己实现。
注意:BMTemplateLayoutCell的实现原理是:内部创建Cell,然后使用Block让外部进行设值和布局,内部使用layoutIfNeeded强制布局,在获取Cell的contentView的最底部的View的最大高度为Cell需要的高度,所以使用者不可对您自定义的Cell设置任何的底部约束限制,所以可能需要您额外增加一个View(记得把它的高度最好设置为0,它的唯一功能是提供布局需求),直接由顶部布局下来即可,放心内部会处理好的
在Cell中必须保证有一个视图的maxY即是Cell的高度,具体可以查看demo中的xib约束设置。
@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;
}];
}
Podfile
中添加 `pod 'BMTemplateLayoutCell'pod install
或 pod update
#import <BMTemplateLayoutCell/UITableView+BMTemplateLayoutCell.h>
pod
无法找到 BMTemplateLayoutCell
可用 pod setup
或 rm ~/Library/Caches/CocoaPods/search_index.json
在 pod search BMTemplateLayoutCell
Clone
或者 download
下载BMTemplateLayoutCell文件夹内的所有内容。BMDragCellCollectionView
使用 MIT 许可证,详情可见 LICENSE 文件