'UICollectionView attempt to create view animation for nil view

I have a UICollectionView with 2 sections. In the first one there is a custom UISegmentedControl with two options: "All" and "Favourite".When I click one of them, I fetch the data from my CoreData, so if I click "All" I fetch data from my entity number 1, if I click "Favourite" I fetch data from my entity number 2. In the second section I have my UICollectionViewCell. When I click the UISegmentedControl, in addition to fetch the data I want to reload the data only in the second section,where I have my cells, and not in all the collectionView. I tried to use the method reloadSections but I get an error: attempt to create view animation for nil view.I can't figure out why.This is my code for the segmentedControl:

func didSelect(_ segmentIndex: Int) {
    if segmentIndex == 1{
        fetchDataFavourite()
        collectionView?.reloadSections([0])
    }else{
        fetchData()
        collectionView?.reloadSections([0])
    }
}


Solution 1:[1]

in my case it was missing

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView

in my implementation of UICollectionViewDataSource.

Solution 2:[2]

I suspect that happens with your custom UICollectionViewLayout because of standard UICollectionViewFlowLayout avoids this issue perfectly.

In case you added supplementary elements into the calculation on prepare() your supplementary header and footer become mandatory.

    private var cache: [UICollectionViewLayoutAttributes] = []
    private var contentSize: CGSize = .zero
    
    // MARK: -

    override func prepare() {
        super.prepare()
        cache.removeAll()
...
        for section in 0 ..< countSections {
...
            attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                                                              with: IndexPath(item: 0, section: section))
            attributes.frame = frame
            cache.append(attributes)
...
            for item in 0 ..< countItems {
                let indexPath = IndexPath(item: item, section: section)
...
                let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
                attributes.frame = rect.integral
                cache.append(attributes)
...
            }
...
            attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
                                                              with: IndexPath(item: 0, section: section))
            attributes.frame = frame
            cache.append(attributes)
...
        }
    }

In order to make them optional - avoid adding their attributes (even empty) in the calculation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mateusz
Solution 2 Alexey Ishkov