TableView单元格高度不是根据第一次重新加载时的collectionview高度

时间:2018-04-20 04:22:21

标签: ios uitableview uicollectionview swift4 uitableviewautomaticdimension

我知道很多线程已经提到过这个问题,但没有一个解决方案对我有用。问题场景是:我在tableview单元格中有一个collectionview。对集合视图的约束如下: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()
}

在按钮上单击表视图重新加载,当打印出collectionview高度约束值时,它会成立,但tableview单元格未获得确切的内容大小。但是当我滚动时,tableview单元格会得到确​​切的大小。

PS:还尝试使用:

collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height

2 个答案:

答案 0 :(得分:0)

collectionviewCell中,您需要使用ContentView并设置常量高度

    self.contentView.translatesAutoresizingMaskIntoConstraints = false
    Heightconstant.constant = 200

答案 1 :(得分:0)

通过设置收集流程布局来尝试这个: -

var flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: 200, height: collectionView.contentSize.height)
flowLayout.minimumInteritemSpacing = 0
flowLayout.scrollDirection = .vertical
CollectionVW.collectionViewLayout = flowLayout