2

I am trying to create headers for my collectionView with dynamic height. But when I implement "referenceSizeForHeaderInSection", the app crashes with the following exception:

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

Below is the code for the collectionView header:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return feedArray.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width - 20 , height: 70)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FeedHeader", for: indexPath) as! FeedHeader

    return header
}

FeedHeader class is empty as I haven't added any labels to the header yet:

class FeedHeader: UICollectionReusableView
{

}

When I remove referenceSizeForHeaderInSection implementation, the app works without any issues.

Have linked the header to the class as well in the storyboard. What might be the problem?

Thanks in advance.

Vyshakh
  • 632
  • 1
  • 11
  • 29

3 Answers3

1

Got the issue.It was due to the reason that the collectionView was taking the reusable view as footer instead of header. Changed it to header and now the app runs fine. Thanks for the help all.

Vyshakh
  • 632
  • 1
  • 11
  • 29
0

Check if you have registered forSupplementaryViewOfKind.

if not then try to register your supplementaryView in the viewDidLoad

collectionView.register(nibName, forSupplementaryViewOfKind: "String", withReuseIdentifier: "Identifier")
Hakikat Singh
  • 285
  • 2
  • 11
  • The app works fine when I remove referenceSizeForHeaderInSection implementation. I dont the it is a problem with registering the header. – Vyshakh Sep 14 '17 at 05:25
  • 1
    For more clarification read this https://stackoverflow.com/a/29656061/8432814 answer – Hakikat Singh Sep 14 '17 at 05:35
0

Make sure your reuse identifier same as FeedHeader

Check following:

  1. select your header view in Stoaryboard

identifier

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

    let headerView: TitleHeaderCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TitleHeaderCollectionReusableView", for: indexPath as IndexPath) as! TitleHeaderCollectionReusableView
 return headerView
}

also implement your preferred height

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

            return CGSize(width: collectionObj.frame.size.width, height: 50)
    }
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • I have implemented all the suggestions mentioned. Still it gives the same error. However if i remove referenceSizeForHeaderInSection, the app works fine. – Vyshakh Sep 14 '17 at 05:23