在UICollectionViewCell iOS Swift 4中注册标题时出错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在UICollectionViewCell iOS Swift 4中注册标题时出错相关的知识,希望对你有一定的参考价值。

我正在做Let的内置应用程序的Twitter重拍教程,我不断遇到这个错误:'无法出列类型的视图:带标识符headerId的UICollectionElementKindSectionFooter - 必须为标识符注册一个nib或类或连接一个原型单元格故事板”。我尝试注册下面的标题,但我的应用程序仍然给我同样的错误。这是视频的链接:https://www.youtube.com/watch?v=2fcf9yFe944&list=PL0dzCUj1L5JE1wErjzEyVqlvx92VN3DL5

let cellId = "cellId"
let headerId = "headerId"

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.backgroundColor = .white
    collectionView?.register(WordCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
}
答案

您将该视图注册为UICollectionElementKindSectionHeader,但正尝试将其用作UICollectionElementKindSectionFooter。这些细节是关键。

您可以通过执行以下操作来检查以确保collectionView方法获得正确的类型:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  if kind == UICollectionElementKindSectionHeader {
     //Now you can dequeue your view with reuseId "headerId"
  } else {
     //This is where you set the footerView, if you are using one
  }

以上是关于在UICollectionViewCell iOS Swift 4中注册标题时出错的主要内容,如果未能解决你的问题,请参考以下文章

iOS swift UICollectionviewcell 快照错误

如何在 iOS 中使用 Storyboard 创建自定义 UICollectionViewCell?

如何在 iOS 的 UICollectionViewCell 的左上角添加按钮?

具有自动调整大小的 UICollectionViewCell 在 iOS 9 中不起作用,但在 iOS 10 中起作用

iOS:长按时在 uicollectionViewcell 上显示带有文本的标签

iOS:如何更改 UICollectionViewCell 的方向?