ios - CollectionView 方法 'referenceSizeForHeaderInSection' 引发异常

标签 ios swift3 uicollectionview uicollectionreusableview uicollectionviewdelegateflowlayout

我正在尝试为我的 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/

相关文章:

ios - 如何在 Swift 中以编程方式调整图像的 x 轴和 y 轴?

arrays - Swift 数组初始化 : data[section][row]

ios - UICollectionView - 仅在需要时注册 Nib

ios - 修改全局变量内部闭包(Swift 4)

ios - 格式化为 2 位小数 (NSDecimalNumber)

objective-c - 使用 UIViewController 作为协议(protocol)

swift - 使用 Process() 从 git 版本导出文件

ios - 如何使用 UIPickerView 的选定值作为通知的时间间隔?

ios - 快速在单个 Storybord 中使用 2 个不同的 UICollectionView

ios - UIImageView 溢出到 Collection View 单元格之外