Nia*_*dle 5 dynamic uitableview uicollectionview swift
我有一个动态的集合视图,其中可以包含任意数量的单元格。它位于表格视图单元格内。
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
我的表格视图已正确配置为动态,因为其他单元格工作正常,因此问题不存在。
这是我目前拥有的......正如你所看到的,下面没有集合视图
这就是我想要的结果...... (强制对集合视图进行高度约束)
我已经在集合视图的所有方面正确配置了约束,因为它在我给出固定高度约束时起作用。但这打败了动态集合视图的对象......
我已经将一个插座链接到UICollectionViewFlowLayout并设置了一个估计的单元格大小,并在内部标签上为单元格提供了正确的约束,正如你在我强制高度约束的图像中看到的那样。
为 collectionView 创建一个子类并覆盖 internalContentSize。
class DynamicCollectionView: UICollectionView {
override func layoutSubviews() {
super.layoutSubviews()
if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return collectionViewLayout.collectionViewContentSize
}
}
Run Code Online (Sandbox Code Playgroud)
在界面生成器中,将 collectionView 的类更改为 DynamicCollectionView(子类 UICollectionView)。
设置 UICollectionViewFlowLayout 的估计单元格大小。
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
Run Code Online (Sandbox Code Playgroud)