我正在尝试为我的 collectionView 创建具有动态高度的 header 。但是当我实现“referenceSizeForHeaderInSection”时,应用程序崩溃并出现以下异常:
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'
下面是 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 FeedHeader: UICollectionReusableView
{
}
当我删除 referenceSizeForHeaderInSection 实现时,该应用可以正常运行。
在 Storyboard 中也将标题链接到类。 可能是什么问题?
提前致谢。
最佳答案
得到问题。这是由于 collectionView 将可重用 View 作为页脚而不是页眉的原因。将其更改为 header ,现在应用程序运行正常。感谢大家的帮助。
关于ios - CollectionView 方法 'referenceSizeForHeaderInSection' 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46210733/