Kyle Gillen Kyle Gillen - 19 days ago 7
Swift Question

viewForSupplementaryElementOfKind crashes app searching for UICollectionElementKindSectionFooter (which I'm not requesting)

I'm using Xcode 8.2, and getting the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindSectionFooter with identifier Header - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'


Here is the function causing the issue:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)
}


Initially it was having an issue finding UICollectionElementKindSectionHeader, but now seems to be stuck on the footer view, which is odd because I haven't actually activated it in Interface Builder:

Image of IB

The reuse identifier for the supplementary (collection reusable view) matches the string in the function. I've tried removing the app from the simulator to no avail. I'm not using any separate nibs or anything out of the ordinary.

I'd done this before in the past with no issue, not sure if this is an Xcode bug or if I've gone awry somewhere.

Any help is very much appreciated. Thanks.

Update

Thanks to the replies and comments I attempted to add the following to the
viewDidLoad
method:

self.collectionView?.register(UINib(nibName:"Header", bundle: Bundle.main), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:"Header")


Which prompted an additional error:

Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewC‌​ategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UI‌​Kit-3600.5.2/UIColle‌​ctionView.m:4922


Resolved

Sorry folks. It was a stupid oversight on my part.
@matt pointed out that I'd implemented an erroneous method:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {


When I meant to implement:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {


Basically, that's where all the confusion stemmed from.

Kindly ignore me. Thanks again.

Answer

You have implemented

func collectionView(_ collectionView: UICollectionView, 
   layout collectionViewLayout: UICollectionViewLayout, 
   referenceSizeForFooterInSection section: Int) -> CGSize {

Delete that method. You have no footer.

Also you have said

self.collectionView?.register(UINib(nibName:"Header", bundle: Bundle.main), 
    forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, 
    withReuseIdentifier:"Header")

Delete that line. You must not register when you are getting your object from the storyboard, and in any case you have no nib named "Header".

Your app will then launch without crashing.

Comments