基本思路来自这个 StackOverflow 问题:http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights (中文)。
因此,这个库旨在简化自动布局视图的高度计算和缓存。我们在这个库中提供了两个组件:MFViewHeightCache 和 UITableView+CellHeightCalculation。
MFViewHeightCache 提供了一种简单的方式来计算和缓存任何类型的自动布局视图的高度。
它基本上在 UIView 上添加了一个类别方法来计算视图的高度,然后在上述方法之上构建了一个缓存。
- (CGFloat)mf_heightForWidth:(CGFloat)width
withHeightCalculationView:(nullable UIView *)view
configuration:(nullable void(^)(void))configuration {
NSAssert(self.translatesAutoresizingMaskIntoConstraints, @"View must be auto layout enabled.");
if (configuration != nil) {
configuration();
}
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[self setBounds:CGRectMake(0.0, 0.0, width, CGFLOAT_MAX)];
[self setNeedsLayout];
[self layoutIfNeeded];
return ceil([(view ?: self) systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
}
基本用法是首先缓存视图,然后调用高度计算方法。例如
// Let's say you have a heightCache property in your view controller.
@property (nonatomic, strong) MFViewHeightCache *heightCache;
// Cache view first.
- (MFViewHeightCache *)heightCache {
if (_heightCache == nil) {
_heightCache = [[MFViewHeightCache alloc] init];
BookCell *cell = (BookCell *)[[[NSBundle mainBundle]
loadNibNamed:NSStringFromClass([BookCell class])
owner:nil
options:nil]
firstObject];
[_heightCache cacheView:cell
withKey:NSStringFromClass([BookCell class])
heightCalculatedByView:cell.contentView];
}
return _heightCache;
}
// Then use heightCache to calculate and cache view's height.
__weak typeof(self) weakSelf = self;
const CGFloat height = [self.heightCache
heightForViewWithKey:NSStringFromClass([BookCell class])
width:CGRectGetWidth(tableView.bounds)
heightCacheKey:dataDict[@"ISBN"]
viewConfiguration:^(__kindof UIView * _Nonnull view) {
[weakSelf _configureCell:(BookCell *)view
withDict:dataDict];
}] + 1.0;
或者,如果您不需要缓存,您可以直接使用以下 UIView 的类别方法之一来计算视图的高度
- (CGFloat)mf_heightForWidth:(CGFloat)width;
- (CGFloat)mf_heightForWidth:(CGFloat)width
configuration:(nullable void(^)(void))configuration;
- (CGFloat)mf_heightForWidth:(CGFloat)width
withHeightCalculationView:(nullable UIView *)view
configuration:(nullable void(^)(void))configuration;
UITableView+CellHeightCalculation 是基于 MFViewHeightCache 的一个 UITableViewCell 类别。
API 非常简单,基本上您只需在 UITableViewDelegate 的 -tableView:heightForRowAtIndexPath:
方法中执行操作即可。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView mf_heightForCellWithIdentifier:@"cell reuse identifer" cellConfiguration:^(__kindof UITableViewCell * _Nonnull cell) {
//Configure cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
}];
}
UITableView+CellHeightCalculation 在 MIT 许可证下分发。
The MIT License (MIT)
版权所有 © 2016 Tinghui
以下是对使用本软件及其相关文档文件(“软件”)的人的一种授权,授权免费获得该软件的副本,不得限制使用、复制、修改、合并、出版、分发、再许可和/或销售软件副本,并允许向获得软件的人提供本软件副本,前提是必须遵守以下条件
在软件的所有副本或主要部分中应包含上述版权声明和本许可声明。
该软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论是在合同行为、侵权行为或其他情况下,不论是否由于软件或其使用或其他与软件相关的情况而引起。