UITableView-BMTemplateLayoutCell 2.0.0

UITableView-BMTemplateLayoutCell 2.0.0

idhong 维护。



  • 作者:
  • liangdahong

UITableView-BMTemplateLayoutCell

对 UITableView 的 Cell 和 UITableViewHeaderFooterView 进行高度自适应和缓存,同时完美支持 Storyboard、Xib、Masonry,以及注册方式或者 alloc 方式创建 Cell

声明

此库代码参考了 FDTemplateLayoutCell,在此表示感谢。

写在前面

由于最近需要做一个类似微信聊天窗口的功能,涉及到 UITableViewCell 的高度自适应,同时需要准确获取 UITableViewCell 的 contentSize 并做相应的滚动和动画,在反复尝试使用系统自适应和 FDTemplateLayoutCell 均有一些问题处理,代码计算又太麻烦,因此尝试自己实现。

原因

  1. 系统自动计算无法满足准确获取 contentSize。
  2. 系统自动计算效率较低(来回反复计算)导致滚动指示器跳动。
  3. 系统自动计算对UITableViewHeaderFooterView的自适应不太友好。
  4. FDTemplateLayoutCell 似乎在 iOS10.2.1 会崩溃,而且似乎有一些小问题。
  5. 代码计算太繁琐容易出错,您可以试试使用。

存在的问题

  1. 此库可能会额外增加一个 View 提供布局所需(ps:如果自定义了分割线则实际上不需要增加)

使用说明

注意:BMTemplateLayoutCell的实现原理是:内部创建Cell,然后使用Block让外部进行设值和布局,内部在使用layoutIfNeeded强制布局,在获取Cell的contentView的最底部的View的最大高度为Cell需要的高度,所以使用者不可对自定义的Cell设置任何的底部约束限制,所以可能需要额外增加一个View(记住把它的高度最好设置为0,它的唯一功能就是提供布局需要的空间),直接从顶部布局下来即可,放心内部会处理妥当。

Cell中必须保证有一个视图的最大Y即是Cell的高度,具体可以查看demo中的xib约束设置。

  1. 支持xibmasonry
  2. 支持使用注册方式alloc方式创建Cell

如果您的TableView支持横竖屏,请设置以下属性为YES,支持IB和代码设置。

@property (assign, nonatomic, getter=isScreenRotating) IBInspectable BOOL screenRotating; ///< 是否支持屏幕旋转,默认NO

存在的问题

  1. 部分代码暂未优化,以后会进行优化。

例子

Cell

gif动画效果图

Xib约束效果图

代码设置

- (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;
    }];
}

UITableViewHeaderFooterView

gif效果图

Xib约束图

代码设置

- (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

推荐使用 CocoaPods 安装,CocoaPods 的使用请点击此处链接1、链接2、链接3

  1. Podfile 中添加 `pod 'BMTemplateLayoutCell'
  2. 执行 pod installpod update
  3. 在需要使用的地方导入
  4. #import <BMTemplateLayoutCell/UITableView+BMTemplateLayoutCell.h>
  5. 如果pod无法找到可用的BMTemplateLayoutCell,可以使用pod setuprm ~/Library/Caches/CocoaPods/search_index.json,然后在pod search BMTemplateLayoutCell中查找。

手动安装

  1. 通过Clonedownload下载BMTemplateLayoutCell文件夹中的所有内容。
  2. BMTemplateLayoutCell内的源文件添加(拖放)到您的项目中。
  3. 导入"UITableView+BMTemplateLayoutCell.h"

期待

  • 如果在使用过程中遇到BUG,希望您能通过Issues反馈给我。

版本历史

  • v1.0.9
  • v1.0.8
  • v1.0.7
  • v1.0.6
  • v1.0.5
  • v1.0.4
  • v1.0.3
  • v1.0.2
  • v1.0.1
  • v1.0.0

MIT

BMDragCellCollectionView使用MIT许可证,详细信息请参阅LICENSE文件。