【发布时间】:2017-09-01 12:46:02
【问题描述】:
我已经用UITableViewAutomaticDimension 设置了UITableViewCell
TableViewCell 中嵌入了一个 UICollectionView,它不可滚动,但可以根据 collectionview 的内容具有可变的高度。
现在我尝试的是渲染单元格并将集合视图的高度约束分配给变量collectionViewHeightConstraint,然后在 layoutSubviews 方法中渲染集合视图后更新高度
override func layoutSubviews() {
super.layoutSubviews()
self.collectionViewHeightConstraint?.constant = self.collectionView!.contentSize.height
}
这是 collectionview 约束的样子(使用制图):
self.contentView.addSubview(self.collectionview)
self.contentView.addSubview(self.holdingView)
constrain(self.holdingView!, self.contentView) {
holderView, container in
holderView.top == container.top
holderView.left == container.left
holderView.right == container.right
holderView.bottom == container.bottom
}
constrain(self.collectionView!, self.holdingView) {
collectionView, containerView in
collectionView.top == containerView.top
collectionView.left == containerView.left
collectionView.right == containerView.right
collectionViewHeightConstraint = collectionView.height == collectionViewHeight
containerView.bottom == collectionView.bottom
}
但这似乎并没有更新单元格高度。
有什么方法可以更新吗?
编辑
这不是某些人建议的重复问题,为什么在下面的 cmets 中进行了解释。
【问题讨论】:
-
在设置高度后尝试重新加载
UITableView并检查heightConstant != contentSize是否存在。使用它来检查UICollectionView的高度是否正确更新。 -
@Rikh :这听起来像是一个解决方案,但也非常混乱和骇人听闻。我的意思是,如果没有别的,这就是我会去的。
-
好吧,你确实需要通知你的
UITableView重新计算它的高度,只有在你知道你更新了哪个集合视图的情况下,你总是可以尝试重新加载特定的行(如果你不想重新加载整个UITableView)! -
@Rikh 它仍然很混乱,因为 layoutsubviews 被多次调用,有时内容高度为 0
标签: ios swift uitableview cocoa-touch xib