Search code examples
swiftuicollectionviewsizeheightuicollectionviewcell

Get the sum of all collectionViewCells' heights inside a CollectionView


I have a CollectionView created inside CollectionView cell class.

The thing is I can't get the CollectionView that lives inside the first collectionView get the perfect size to fit all cells' sizes if any.

enter image description here

First level comments Line is the First CollectionView Cell, enclosing the second level comments(contentView is blue and the collectionView holding the cells is red)

I am using constraints to set the second collectionView height. It's basically fixed now.

I know how to make the comments cells have dynamic height, but how can I make the second collectionView have dynamic height to fit all cells if any?

Possible solution, doing something with:

for cell in collectionView.visibleCells as! [MyCellClass] {
   let sumOfAllCells += cell.frame.height 

}

Solution

  • var height: CGFloat = 0
    collectionView.visibleCells.forEach({ height += $0.bounds.height })