1

J'essaie de créer des en-têtes pour ma collectionView with dynamic height. Mais quand je mets en œuvre « referenceSizeForHeaderInSection », les application se bloque à l'exception suivante:La méthode CollectionView 'referenceSizeForHeaderInSection' génère une 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' 

est Ci-dessous le code de l'en-tête CollectionView:

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 
} 

classe FeedHeader est vide comme je l'ai pas ajouté étiquettes à l'en-tête encore:

class FeedHeader: UICollectionReusableView 
{ 

} 

Lorsque je supprime la mise en œuvre de referenceSizeForHeaderInSection, l'application fonctionne sans aucun problème.

Vous avez également lié l'en-tête à la classe dans le storyboard. Quel pourrait être le problème?

Merci d'avance.

Répondre

1

Got the issue.It était dû à la raison pour laquelle l'CollectionView prenait la vue réutilisable pied de page au lieu d'en-tête. Changement à l'en-tête et maintenant l'application fonctionne bien. Merci pour l'aide tous.

0

Vérifiez si vous avez enregistré pourSupplementaryViewOfKind.

si essayez donc pas d'enregistrer votre supplementaryView dans le viewDidLoad

collectionView.register(nibName, forSupplementaryViewOfKind: "String", withReuseIdentifier: "Identifier") 
+0

L'application fonctionne correctement lorsque je supprime l'implémentation referenceSizeForHeaderInSection. Je ne suis pas le problème avec l'enregistrement de l'en-tête. – Vyshakh

+0

avez-vous essayé le code supérieur? @Vyshakh –

+1

Pour plus de précisions lire ce https://stackoverflow.com/a/29656061/8432814 répondre –

0

Assurez-vous que votre identifiant de réutilisation même que FeedHeader

Vérifiez les points suivants:

  1. sélectionnez votre vue d'en-tête dans 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 
} 

également mettre en œuvre votre hauteur préférée

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

      return CGSize(width: collectionObj.frame.size.width, height: 50) 
    } 
+0

J'ai mis en œuvre toutes les suggestions mentionnées. Pourtant, il donne la même erreur. Cependant, si je supprime referenceSizeForHeaderInSection, l'application fonctionne correctement. – Vyshakh