使用masonry布局時,UITableViewCell自適應高度工具
HYBMasonryAutoCellHeight
github:https://github.com/CoderJackyHuang/HYBMasonryAutoCellHeight
有關講解
Masonry自動計算行高:http://101.200.209.244/masonry-cell-height-auto-calculate/
有關使用
-
添加布局位置
UI布局必須放在UITableViewCell的初始化方法中 -initWithStyle:reuseIdentifier: 且必須指定hyb_lastViewInCell才能生效
UITableViewCell中設置Cell最底部控件, 如有需要還可以設置最底部控件距離cell底部的高度,如下:
/**
* 必傳設置的屬性,也就是在cell中的contentView內(nèi)最后一個視圖磁椒,用于計算行高
* 例如搔课,創(chuàng)建了一個按鈕button作為在cell中放到最后一個位置阁猜,則設置為:self.hyb_lastVieInCell = button;
* 即可。
* 默認為nil雷滚,如果在計算時,值為nil,會crash
*/
@property (nonatomic, strong) UIView *hyb_lastViewInCell;
/**
* 可選設置的屬性篮迎,默認為0,表示指定的hyb_lastViewInCell到cell的bottom的距離
* 默認為0.0
*/
@property (nonatomic, assign) CGFloat hyb_bottomOffsetToCell;
- UITableViewDelegate中調(diào)用
/**
* 通過此方法來計算行高示姿,需要在config中調(diào)用配置數(shù)據(jù)的API
*
* @param indexPath 必傳甜橱,對應的indexPath
* @param confi 必須要實現(xiàn),且需要調(diào)用配置數(shù)據(jù)的API
*
* @return 計算的行高
*/
+ (CGFloat)hyb_heightForIndexPath:(NSIndexPath *)indexPath config:(HYBCellBlock)config;
調(diào)用方式如下
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
HYBNewsModel *model = nil;
if (indexPath.row < self.dataSource.count) {
model = [self.dataSource objectAtIndex:indexPath.row];
}
return [HYBNewsCell hyb_heightForIndexPath:indexPath config:^(UITableViewCell *sourceCell) {
HYBNewsCell *cell = (HYBNewsCell *)sourceCell;
// 配置數(shù)據(jù)
[cell configCellWithModel:model];
}];
}