2

我正在尝试为我的 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 实现时,该应用程序可以正常工作。

已将标题链接到故事板中的课程。可能是什么问题?

提前致谢。

4

3 回答 3

1

得到了问题。这是由于 collectionView 将可重用视图作为页脚而不是页眉的原因。将其更改为标题,现在应用程序运行良好。感谢大家的帮助。

于 2017-09-14T05:34:08.497 回答
0

检查您是否已注册forSupplementaryViewOfKind

如果没有,请尝试在viewDidLoad中注册您的补充视图

collectionView.register(nibName, forSupplementaryViewOfKind: "String", withReuseIdentifier: "Identifier")
于 2017-09-14T04:57:38.773 回答
0

确保您的重用标识符与FeedHeader

检查以下内容:

  1. 在故事板中选择您的标题视图

标识符

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
}

也实现你喜欢的高度

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

            return CGSize(width: collectionObj.frame.size.width, height: 50)
    }
于 2017-09-14T05:09:29.617 回答