0
votes

I know this is already asked in many threads but none of the solutions are working for me. Problem scenario is: I have a collectionview in tableview cell. The constraints on collection view are as: Collectionview top, leading, trailing and bottom constraint to superview and heightconstraint (with priority 999). Changing the height of collection view as:

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()
}

On a button click the tableview is reload and when collectionview height constraint value is printed, it comes true but the tableview cell is not getting exact content size. But when i scroll then the tableview cell gets the exact size.

PS: Also tried using :

collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
2
Need to reload table view after collection view reload.Scott.N
@Scott.N but it will lead to the reload of tableview two timesS.V
Consider reload table cell row container collectionviewScott.N
@Scott.N but where do i need to reload tableview row as collectionview is in tableview cell.S.V
At the place that you call reload collectionviewScott.N

2 Answers

0
votes

In your collectionviewCell, you need to take ContentView and set constant Height

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

try this by setting collection flow layout:-

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