一个非常有用的类别,可用于自动计算单元格的高度。
在 github 上未能找到基于 Masonry 自动计算行高的库,倒是找到了使用 xib 和 storyboard 在添加约束来自动计算行高的库,如:UITableView-FDTemplateLayoutCell
本人非常推崇 Masonry 来实现代码的自动布局,因此项目中都是使用 Masonry 布局的,为了自动计算行高,决定写一个扩展,以达到自动计算的效果,如此一来,开发者不用再关心那些非固定行高的 cell 的动态计算问题了。
只需在UITableView的代理方法中调用:
/**
* 通过此方法来计算行高,需要在config中调用配置数据的API
*
* @param indexPath 必传,对应的indexPath
* @param confi 必须要实现,且需要调用配置数据的API
*
* @return 计算的行高
*/
+ (CGFloat)hyb_heightForIndexPath:(NSIndexPath *)indexPath config:(HYBCellBlock)config;
调用方式类似如下:
- (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;
// 配置数据
[cell configCellWithModel:model];
}];
}
具体使用和具体 API 说明,请参考 Demo!
个人博客地址:http://101.200.209.244.com/
CSDN博客地址:http://blog.csdn.net/woaifen3344
个人常用邮箱:[email protected]
感谢 UITableView-FDTemplateLayoutCell 的作者 sunnyxx 给了我灵感,这才有现在的基于 Masonry 版本的自动计算行高的开源库。感谢所有使用本开源库的开发者,感谢你们的使用和反馈。
如果在使用过程中遇到问题,或者想要与我交流,可加入有问必答 QQ 群:324400294
关注微信公众号:iOSDevShares
关注新浪微博账号:标哥Jacky
MIT