UICollectionViewCompositionalLayout + UICollectionViewDiffableDataSource: is there a way to change a decoration view depending on section data?

Viewed 555

I have a UICollectionView that uses UICollectionViewCompositionalLayout and UICollectionViewDiffableDataSource and it works fine.

Cells are provided by a cellProvider defined in the initialiser of the datasource. The provider returns cells with indexPath specific data.

Supplementary views are provided by a var supplementaryViewProvider: UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.SupplementaryViewProvider

Only problem: I did not find something like a DecorationViewProvider:

Following the WWDC 2019 example, I use

layout.register(SectionBackgroundDecorationView.self, forDecorationViewOfKind: 
    MyCompositionalCollectionViewController.sectionBackgroundDecorationElementKind)

and

        let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
            elementKind: MyCompositionalCollectionViewController.sectionBackgroundDecorationElementKind)
        section.decorationItems = [sectionBackgroundDecoration]

to provide a background view for a section.

Problem: Since I did not find a provider, the background decoration view is initialised automatically so I did not find a way to change it depending on section data.

For example I'd like to set a different color to the background decoration view depending on the section.

How do I let the background decoration view know which section it belongs to or how do I set properties depending on the section?

(Only dirty hack that comes to my mind: I could write 20 different classes SectionBackgroundDecorationView1, SectionBackgroundDecorationView2, use a different forDecorationViewOfKind: for each and each class has hard coded that it belongs to section 0, section 1 ... there has to be a better solution)

0 Answers
Related