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