Collection-View-in-a-Table-View-Cell icon indicating copy to clipboard operation
Collection-View-in-a-Table-View-Cell copied to clipboard

None scrollable CollectionView, dynamic height depending on content.

Open hugocarlmartin opened this issue 9 years ago • 5 comments

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

hugocarlmartin avatar Feb 08 '16 12:02 hugocarlmartin

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. :)

hugocarlmartin avatar Feb 08 '16 14:02 hugocarlmartin

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 :)

ashfurrow avatar Feb 08 '16 16:02 ashfurrow

Hi! @hugocarlmartin, have you found a solution for this task?

vpetrenko90 avatar Dec 01 '16 23:12 vpetrenko90

@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 avatar Dec 02 '16 08:12 hugocarlmartin

@hugocarlmartin , the idea is to get rid of a scroll and show all collection cells inside table cell :)

vpetrenko90 avatar Dec 02 '16 14:12 vpetrenko90