【发布时间】:2018-04-20 04:22:21
【问题描述】:
我知道这已经在很多线程中被问过了,但没有一个解决方案对我有用。问题场景是:我在表格视图单元格中有一个集合视图。集合视图的约束如下:Collectionview 顶部、前导、尾随和底部约束到 superview 和 heightconstraint(优先级 999)。将集合视图的高度更改为:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GalleryCollectionViewCell.identifier, for: indexPath) as? GalleryCollectionViewCell {
collectionViewHeightConstraint.constant = collectionView.contentSize.height
self.layoutIfNeeded()
return cell
}
return UICollectionViewCell()
}
点击按钮时,tableview 会重新加载,当打印 collectionview 高度约束值时,它会实现,但 tableview 单元格没有获得准确的内容大小。但是当我滚动时,tableview 单元格会得到确切的大小。
PS:也试过使用:
collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
【问题讨论】:
-
集合视图重新加载后需要重新加载表格视图。
-
@Scott.N 但它会导致两次重新加载tableview
-
考虑重新加载表格单元格行容器集合视图
-
@Scott.N 但是我需要在哪里重新加载 tableview 行,因为 collectionview 在 tableview 单元格中。
-
在你调用reload collectionview的地方
标签: ios uitableview uicollectionview swift4 uitableviewautomaticdimension