UICollectionViewCell 无法使类型的视图出列:具有标识符的 UICollectionElementKindCell

UICollectionViewCell could not dequeue a view of kind : UICollectionElementKindCell with identifier

我正在关注 Duc Tran 关于集合的教程 (https://www.youtube.com/watch?v=vB-HKnhOgl8),当我 运行 我的应用程序时,我不断收到此错误:

线程 1:异常:“无法使类型的视图出队:具有标识符 CHALLENGECELL 的 UICollectionElementKindCell - 必须为标识符注册一个笔尖或 class 或在故事板中连接一个原型单元格”

我为此查看了其他解决方案,但 none 已经产生了结果。

这是保存集合的视图控制器的代码

import UIKit

class ChallengesViewerViewController: UIViewController {



@IBOutlet weak var collectionView: UICollectionView!




//MARK: - UICollectionViewDataSource
private var challenges = WeeklyChallenges.createChallenge()
let cellScaling: CGFloat = 1.0

override func viewDidLoad() {
    super.viewDidLoad()

    let screenSize = UIScreen.main.bounds
    let cellWidth = floor(screenSize.width * cellScaling)
    let cellHeight = floor(screenSize.height * cellScaling)
    
    let insetX = (view.bounds.width - cellWidth)/2
    let insetY = (view.bounds.height - cellHeight)/2
    
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    
    layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
    
    collectionView.contentInset = UIEdgeInsets(top: insetY, left: insetX, bottom: insetY, right:    insetX)
    
    collectionView.dataSource = self

}
  

}

 extension ChallengesViewerViewController:     UICollectionViewDataSource{

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return challenges.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CHALLENGECELL", for: indexPath) as! ChallengeCollectionViewCell
   
    
    cell.challenge = self.challenges[indexPath.item]
    
    return cell
} 

}

此外,here 是故事板,我在其中将可重用标识符分配给集合视图单元格。

您必须 register 您要在 cellForItem 中出队的单元格 viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    //...
    collectionView.register(ChallengeCollectionViewCell.self, forCellWithReuseIdentifier: "CHALLENGECELL")
}

如果您使用的是 Nib,请改用:

let nib = UINib(nibName: <#T##String#>, bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "CHALLENGECELL")