我正在做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
}