计算tableView的heightForRowAtIndexPath中的UICollectionView contentSize

Calculate UICollectionView contentSize in tableView's heightForRowAtIndexPath

我在 UITableViewCell 中有一个 collectionView。 collectionView 的内容是动态的,因此它的高度也是动态的,tableView 单元格的高度取决于 collectionView 的高度。 collectionView 单元有不同的大小。 collectionView 的高度始终设置为适合其 contentSize 高度。

我的问题是 tableView 的函数 heigthForRowAtIndexPath 在创建 collectionView 之前被调用(在 cellForRowAtIndexPath 中),因此,为了 return 适合 collectionView 的高度,我需要手动计算它(这不是'对于具有不同大小的单元格的 collectionView 来说,这似乎不是一个好主意。

我尝试使用自动布局来使用 UITableViewAutomaticDimension,但它不起作用(也许我做错了)。

让 UITableViewCell 在 heightForRowAtIndexPath 中考虑其子视图高度的最佳方法是什么?我可以在不创建 collectionView 的情况下知道它的估计大小吗?

使用自调整大小,iOS 8. 网上有很多很好的教程,比如这个:http://www.appcoda.com/self-sizing-cells/

我们的想法是,您可以使用自动布局和 viewDidLoad 中的几行代码来呈现动态适合其中内容的 table 视图单元格。

更多教程: