iosswiftuicollectionviewimessage-extension

Header in collection view won't show up? Used to work?


I migrated this project (an iMessage app with a collection view to hold stickers) from Swift 3 to Swift 5, and everything works but the header view function simply will not fire, nor will header show up.

I register the header cell in storyboard:

enter image description here

then in collection view functions:

private func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let headerView: HeaderCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as IndexPath) as! HeaderCollectionReusableView

        print("CALLED")
        //header1 = headerView as! UICollectionReusableView

        return headerView
    }

Ive even tried turning the header a different color in storyboard, however this function doesn't even print to console. What is wrong here? This used to work and the other cells work.


Solution

  • I migrated this project (an iMessage app with a collection view to hold stickers) from Swift 3 to Swift 5

    Well you didn't migrate this line:

    private func collectionView(_ collectionView: UICollectionView, 
    viewForSupplementaryElementOfKind kind: String, 
                     atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    

    Change it to:

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

    Not private. Not atIndexPath. Not NSIndexPath. Talk Swift, not Objective-C, and don't hide this method from Objective-C or it can't be called by Cocoa.

    Plus make absolutely sure you are in a place where adoption of UICollectionViewDataSource is declared (you probably are, since you say other methods are being called okay).