UICollectionView and Supplementary View (header)

前端 未结 10 1360
南旧
南旧 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:44

    I also got this annoying error:

    *** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-3347.44/UICollectionView.m:1400
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
    

    and I found some people solving this error by doing

    - (void)dealloc {
        self.collectionView = nil;
    }
    

    or in swift:

    deinit {
        collectionView = nil
    }
    

    however neither of these solved my crash. I tried a few things out and the following "hack" solved this annoying bug:

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if collectionView.dataSource != nil {
            return someHeaderSize
        } else {
            return CGSizeZero
        }
    }
    

提交回复
热议问题