我正在尝试为我的 collectionView 创建具有动态高度的标题。但是当我实现“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 标头的代码:
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 实现时,该应用程序可以正常工作。
已将标题链接到故事板中的课程。可能是什么问题?
提前致谢。