渲染后更新单元格高度

Update cell height after it is rendered

我设置了 UITableViewCellUITableViewAutomaticDimension
TableViewCell 中嵌入了一个 UICollectionView,它不可滚动,但可以根据 collectionview 的内容具有可变高度。

现在我尝试的是渲染单元格并将 collectionview 的高度约束分配给变量 collectionViewHeightConstraint 然后在 layoutSubviews 方法中渲染 collectionview 后更新高度

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
    }

但这似乎并没有更新单元格高度。

有什么方法可以更新吗?

编辑
这不是某些人建议的重复问题,原因在下面的评论中有解释。

您可以重新加载 tableview 的特定单元格

let indexPath = IndexPath(item: rowNumber, section: 0)
    tableView.reloadRows(at: [indexPath], with: .top)

按照你的要求,我想如果我们先加载collectionview,然后加载具有collectionview正确高度的tableview,我们就可以解决这个问题。

collectionView.reloadData()
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.collectionViewHeightConstraint?.constant = self.collectionView!.contentSize.height
        })
tableview.reloadData()

当 tableview 加载时,单元格根据集合视图内容大小具有所需的高度。

由于评论太小space,我把所有的都放在这里:

注意:您实际上不必在 viewDidLayoutSubviews 中设置高度限制,只需在某个地方您可以确定 UICollectionView 已设置 您的布局已在整个屏幕上正确设置!例如,在 viewDidAppear 中执行,然后调用 layoutIfNeeded() 也可以。将它移动到 viewDidAppear 只有在调用 viewDidAppear 之前设置了 UICollectionView 才有效,即您事先知道 UICollectionView 数据源。

修正 1:

设置高度并检查 heightConstant != contentSize 后,尝试重新加载 UITableView。使用它来检查 UICollectionView 的高度是否正确更新,即:

override func layoutSubviews() {
    super.layoutSubviews()

    if self.collectionViewHeightConstraint?.constant != self.collectionView!.contentSize.height{

        self.collectionViewHeightConstraint?.constant = self.collectionView!.contentSize.height

        //to make sure height is recalculated
        tableView.reloadData()
        //or reload just the row depending on use case and if you know the index of the row to reload :)
    }
}

我同意你的评论,它很乱,我的意思是用它作为修复 and/or 来检查这是否是问题所在!

至于为什么是0,可能是因为你的UICollectionView还没有设置(cellForItem还没有被调用)所以contentSize不是居然算出来了!

修复 2:

一旦为 UICollectionView 设置了 dataSource,即您收到数据,您将手动计算 UICollectionView contentSize 的高度并设置一次并重新加载该行。如果计算是一项繁琐的工作,只需设置数据源并在 UICollectionView 上调用 reloadData。这将确保 UICollectionView 设置正确,然后将单元格的约束设置为 contentSize 并在 UITableView 上调用 reloadDatareloadRow

您基本上可以在 UICollectionView 设置 视图 布局 后随时设置 heightConstraint。之后您只需要调用 tableView.reloadData() 即可。