TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.