Collection-View-in-a-Table-View-Cell
Collection-View-in-a-Table-View-Cell copied to clipboard
None scrollable CollectionView, dynamic height depending on content.
Hi, great tutorial and example project.
I have drawn an outlet to the Height Constraint on the collectionView. The height is fixed with low priority. When I filled my collectionView I would like to:
cell.collectionViewHeight.constant = collectionView.contentSize.Height
so that the TableViewCell can resize it self with UITableViewAutomaticDimension. But I don't know where to do this. I've used this before just not in a tableViewCell.
Edit:
First time I scroll down the height is 0, next time its works. So I guess the contentSize of the collectionView is set "to" late.
Cheers
If I:
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.collectionViewLayout.invalidateLayout()
collectionViewHeight.constant = collectionView.contentSize.height // 2
}
With layoutIfNeeded() and invalidateLayout() I get the accurate contentSize, but I'm not allowed to reload the cell here am i? And even if I do, its flickers everywhere. :)
Hey! Yeah, the lifecycle for cells is tricky, there's sort of two tracks: one to configure the cell and another to display it. I wish UIKit did a better job separating these two, but alas :)
Hi! @hugocarlmartin, have you found a solution for this task?
@ameli90 I don't even remember what project I was using this for so I can't even look too see what I ended up doing. :)
@hugocarlmartin , the idea is to get rid of a scroll and show all collection cells inside table cell :)