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

Help! How add Header in UICollectionView

Forums > Swift

I just can’t solve the header connection problem. It always goes into error. I didn't find anything on Google

This is register custom header

        collectionDescriptionView.register(
            HeaderForPictureView.self,
            forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
            withReuseIdentifier: HeaderForPictureView.identifierID
        )

Add custom Header

    func collectionView(
        _ collectionView: UICollectionView,
        viewForSupplementaryElementOfKind kind: String,
        at indexPath: IndexPath
    ) -> UICollectionReusableView {
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: HeaderForPictureView.identifierID,
                for: indexPath
            )
            print(headerView)
            return headerView
        case UICollectionView.elementKindSectionFooter:
            let sectionFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderForPictureView.identifierID, for: indexPath)
            return sectionFooter
        default:
            assert(false, "Unexpected element kind")
        }
    }

Error Thread 1: "could not dequeue a view of kind: UICollectionElementKindSectionFooter with identifier HeaderForPictureView.header - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

1      

the only thing that helped was adding a footer I haven't found any other solution

    func collectionView(
        _ collectionView: UICollectionView,
        viewForSupplementaryElementOfKind kind: String,
        at indexPath: IndexPath
    ) -> UICollectionReusableView {
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: HeaderForPictureView.identifierHeaderID,
                for: indexPath
            )
            guard let typedHeaderView = headerView as? HeaderForPictureView else {
                return headerView
            }
            return typedHeaderView
        case UICollectionView.elementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: FooterForPicturesView.identifierHeaderID,
                for: indexPath
            )
            guard let currentFooter = footerView as? FooterForPicturesView else {
                return footerView
            }
            return currentFooter
        default:
            assert(false, "Unexpected element kind")
        }
    }

1      

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!

Reply to this topic…

You need to create an account or log in to reply.

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.