UICollectionVIew里面的一个UITableViewCell如何获得dynamic高度自动布局

我知道还有一个类似的问题 ,但是我想提供更多关于我如何实现这个function的提示,因为原来的问题没有给出任何build议。
我有一个UITableViewCell作为contentView的子视图有一个UICollectionView ,我想在collectionview的contentSize函数中具有单元格高度,表视图单元格是集合视图的委托和数据源。
收集视图应该在垂直stream动中固定而不需滚动,并且应该调整其高度以适应细胞系的数量。
要做到这一点,我已经尝试使用与普通表格视图单元格相同的技术。 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath我给单元格提供它应该显示的数据,然后询问它的单元格高度为压缩的大小。
类似的东西:

 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGSize size = CGSizeZero; NSDictionary * data = self.data[indexPath.row]; if (data[KEY_CELL_IDENTIFIER] == CellIdentifierPost) { NSNumber * cachedHeight = [self.heightCaches objectForKey:[(PostObject*)data[KEY_CELL_DATA] postObjectId]]; if (cachedHeight) { return (CGFloat)[cachedHeight doubleValue]; } [_heightCell configureCellWith:data[KEY_CELL_DATA]]; size = [_heightCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; [self.heightCaches setObject:@(size.height) forKey:[(PostObject*)data[KEY_CELL_DATA] postObjectId]]; } else if (data[KEY_CELL_IDENTIFIER] == CellIdentifierComment){ size = (CGSize) { .width = NSIntegerMax, .height = 160.f }; } else { size = (CGSize) { .width = NSIntegerMax, .height = 50.f }; } return size.height; } 

这个方法对于其他单元格来说工作得很好,但是对于这个单元格来说却不是这样,结果是一个完全压缩的单元格,其高度几乎为零。
这个问题似乎是由于内部细胞的放置。 即使喂食数据我强制收集视图重新加载数据,它似乎永远不会调用-collectionView:cellForItemAtIndexPath:这可能是因为单元格仍然不显示。
有工作吗?

我build议你为你的collectionView创build一个高度约束。

然后,根据单元格的数量,可以计算它的高度,最后更新该约束并调用[collectionView setNeedsUpdateConstraints]