UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

UICollectionViewDiffableDataSource optional sections

Forums > iOS

Hi, I'm trying to implement a collection view with the diffable data source. I have some optional advertisment which I would like to put in a separate section of the collection view (for specific layouting etc.). My sections look like this:

enum Section: Int, CaseIterable {
            case topSection
            case midSection
            case advertismentSection
            case bottomSection
        }

The height of the item in the advertismentSection is defined like this:

let heightDimension = NSCollectionLayoutDimension.estimated(200)

this heightDimension is then used for the group height etc.

This works fine when I add items to the advertismentSection, however, when it's empty, I would assume it should have height of 0, but it's not the case, it's 200 regardless if it's empty or not. When I try to make the height 0, I get a warning that it's invalid. Another approach I tried is to simply not add this advertismentSection to the snapshot when I have nothing to show, but this crashes the app because the number of sections in the snapshot needs to be the same as defined in your Section enum.

How should I solve this problem?

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.