UICollectionView and Supplementary View (header)

前端 未结 10 1097
南旧
南旧 2021-02-04 05:17

Trying to add a a Supplementary view into my UICollectionView as a header. I\'m having issues getting it to work.

I use a custom UICollectionViewFlowL

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 05:45

    The answers in this topic are quite old, and do not work in iOS8 and iOS9. If you are still having the described issue, check out the following topic: UICollectionView and Supplementary View crash

    EDIT:

    Here is the answer, in case the link becomes invalid:

    If you are using custom layout in your collection view, check if its datasource is nil in:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    

    The result should look something like this:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
        if (self.collectionView.dataSource != nil) {
            // Your layout code    
            return array;
        }
        return nil;
    }
    

    This change resolves the following issue:

    • Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]

    You can also check the following topic, however it didn't resolve anything in my case:

    Removing empty space, if the section header is hidden in the UICollectionView

    Hope it will help you, and you won't waste as much time as I did.

提交回复
热议问题