前言

UITableView动态行高一直是iOS的一个经典问题,在没有AutoLayout的时代,只能自己计算frame,然后返回给代理,非常痛苦。到了AutoLayout的时代,布局就变得简单多了,甚至于通过系统提供的API都能自动计算出行高。

UITableView+FDTemplateLayoutCell就是sunnyxx大大的一个自动计算行高的框架。只要布局正确,通过它可以自动计算并缓存行高,非常方便。不过在使用上发现一些问题,也尝试去解决了。

过程

需求是这样的,一个类似微博的页面,像这样:
微博图片

这应该是比较经典的布局,内容和图片都是不确定的,行高要根据实际数据计算。九宫格实现方式有很多,我这里是通过UICollectionView去实现的。这样的一个好处就是UICollectionView的高度可以通过它的collectionViewLayout对象获取,啥都不用算。不过会有一个问题,UICollectionView继承自UIScrollView,它的高度没法按照内容来全显示。所以即使布局正确,通过AutoLayout来计算行高也是不包括UICollectionView的,这个问题同样反映在一些UIView控件上。

这就十分蛋疼了,难道还要回到手算frame的时代?当然不是,是我还写啥博客。

我说下解决的几个方法。

方法一(不推荐):手动设置collectionView的高度,可以通过代码或者xib来设置,我这里是xib。

xib

像这样手动指定collectionView的高度,然后赋值数据源的时候更新collectionView高度约束就可以了,让它的高度等于它的contentSize.height,这样就能全部显示了,其它UIView控件也能这么解决。但是这样在计算行高的时候会抛出非常多异常,都是约束的问题。我不是很清楚这是什么原因,按理说计算再后,赋值在前,应该不会这样。而且显示会出一些问题,计算的行高会不正确,有些许误差。

方法二(推荐):既然不能通过这种方式,那就绕个弯吧。去掉高度约束,计算出来的高就不包含collectionView的高。然后再手动加上collectionView的高返回给代理不就行了。不过看下UITableView+FDTemplateLayoutCell的拓展方法:

1
2
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier
configuration:(void (^)(id cell))configuration;

只有一个设置cell数据源的block,正常情况下我们只需要把cell换成我们自己的类,然后赋值模型就行,缓存之类的框架会自动处理好。虽然我们可以获取到缓存高度之后再加上collectionView的高,但是这样还叫啥缓存,缓存就是不需要计算,直接取到就能用,那怎么办呢?
虽然可以通过Method Swizzling黑魔法交换方法实现,但是这并不是最优方法,往往是一些莫名其妙的bug的源泉,作为开发者应该尽量避免这种方式。所以最后我选择了通过分类的方式。思路是在框架计算完高度之后通过block返回,我们自行处理行高,加加减减,然后返回高度让框架缓存。
具体代码:
我们参考下框架这个方法的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier
cacheByIndexPath:(NSIndexPath *)indexPath
configuration:(void (^)(id cell))configuration {
if (!identifier || !indexPath) {
return 0;
}

//命中缓存
if ([self.fd_indexPathHeightCache existsHeightAtIndexPath:indexPath]) {
[self fd_debugLog:[NSString stringWithFormat:@"hit cache by index path[%@:%@] - %@", @(indexPath.section), @(indexPath.row), @([self.fd_indexPathHeightCache heightForIndexPath:indexPath])]];
return [self.fd_indexPathHeightCache heightForIndexPath:indexPath];
}

//计算行高
CGFloat height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration];
[self.fd_indexPathHeightCache cacheHeight:height byIndexPath:indexPath];
[self fd_debugLog:[NSString stringWithFormat: @"cached by index path[%@:%@] - %@", @(indexPath.section), @(indexPath.row), @(height)]];

return height;
}

它先从缓存中寻找行高,命中之后直接返回。否则计算行高,存入缓存,然后返回。所以很简单,我们可以直接复制它的代码。写一个带编辑行高功能的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
typedef CGFloat(^editCellHeightAction)(id cell, CGFloat cellHeight);

- (CGFloat)jh_heightForCellWithIdentifier:(NSString *)identifier
cacheByIndexPath:(NSIndexPath *)indexPath
configuration:(void (^)(id cell))configuration
editAction:(editCellHeightAction)editAction {
if (!identifier || !indexPath) {
return 0;
}
// Hit cache
if ([self.fd_indexPathHeightCache existsHeightAtIndexPath:indexPath]) {
[self fd_debugLog:[NSString stringWithFormat:@"hit cache by index path[%@:%@] - %@", @(indexPath.section), @(indexPath.row), @([self.fd_indexPathHeightCache heightForIndexPath:indexPath])]];
return [self.fd_indexPathHeightCache heightForIndexPath:indexPath];
}

CGFloat height = 0;
//获取缓存中的cell
UITableViewCell *templateLayoutCell = [self fd_templateCellForReuseIdentifier:identifier];
//这里插入编辑行高的代码
if (editAction) {
height = editAction(templateLayoutCell, [self fd_heightForCellWithIdentifier:identifier configuration:configuration]);
}
else {
height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration];
}

[self.fd_indexPathHeightCache cacheHeight:height byIndexPath:indexPath];
[self fd_debugLog:[NSString stringWithFormat: @"cached by index path[%@:%@] - %@", @(indexPath.section), @(indexPath.row), @(height)]];
return height;
}
}

使用起来像这样:

1
2
3
4
5
6
7
8
9
10
11
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView jh_heightForCellWithIdentifier:@"MineCell" cacheByIndexPath:indexPath configuration:^(HeSquareCell *cell) {
//正常赋值数据源
cell.model = self.model;
} editAction:^CGFloat(MineCell *cell, CGFloat cellHeight) {
//cellHeight是上面的block计算后回调过来的 所以直接加上额外的高度即可
//因为缓存的关系这里只会走一次 所以可以放心写
return cellHeight + [cell collectionViewHeightWithModel:self.model];
}];
}

这样高度就能正常显示了,而且也不会抛异常,还能享受框架带来的便利。
UITableView+FDTemplateLayoutCell的接口设计很易于拓展,所以写起来很简单。还有个问题,我发现在使用这个框架的时候,如果_tableView.tableFooterView = [[UIView alloc] init];这句话写在注册cell之前,程序会crash,不造为啥。如果各位有更好的解决思路或者文中有错误的地方欢迎给我留言。