Dynamic item count in UICollectionViewCompositionalLayout possible?

Viewed 1802

I want to have serveral sections in my UICollectionView. In one there should be items that hold a text. With a 'classic' collectionView approach The count of items that are displayed in the same row is dependend on the item width. The collectionView only adds a new line if the items wont fit into the same row. So some rows may have 5 items and some have 3. Like so:

This is what I want

When I use compositional Layout I need to specify the item count. And actually I want the collection view to take care about the count, for serveral sections.

Is that even possible with UICollectionViewCompositionalLayout ?

2 Answers

TLDR

If you are using just one NSCollectionLayoutItem object in your compositional layout (which it looks like you are), then create your layout group using one of these methods (depending on which direction you want the layout to flow:

NSCollectionLayoutGroup.horizontal(layoutSize:subitems:)
NSCollectionLayoutGroup.vertical(layoutSize:subitems:)

Place your NSCollectionLayoutItem object as the only item in the subItems array.

When the collection view is laid out, the number of items you specify in UICollectionViewDataSource's (_:numberOfItemsInSection:) will be used to layout your items.


Slightly longer

I'm assuming you're referring to methods like NSCollectionLayoutGroup.horizontal(layoutSize:subitem:count:). If so then you may be misunderstanding what the count parameter means here.

In a situation where you are using a combination of different NSCollectionLayoutItem objects (with different sizes/layouts), the count parameter here means how many of that item you want in that group for the layout.

When the collection view is loaded/reloaded, whatever counts you provide for UICollectionViewDataSource's (_:numberOfItemsInSection:) will still be rendered – if that count is higher than the counts you specify in your compositional layout then new groups (following your layout) will be created to accommodate the extra items.

Well first of all thanks for taking the time.

I want the cells to be dynamicaly size alog the x axis (width) and then UIKit should put as many as possible into one line. It seems that the count indicated a fixed number of items that can be in one row.

here is my code


    func generateLayout() -> UICollectionViewLayout {
        let itemSize = NSCollectionLayoutSize(
            widthDimension: .estimated(100),
            heightDimension: .absolute(20))

        let fullPhotoItem = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .estimated(20))

        let group = NSCollectionLayoutGroup.horizontal(
            layoutSize: groupSize,
            subitem: fullPhotoItem,
            count: 1)

        let section = NSCollectionLayoutSection(group: group)
        let layout = UICollectionViewCompositionalLayout(section: section)
        return layout
    }
Related