【问题标题】:UICollectionViewCell could not dequeue a view of kind : UICollectionElementKindCell with identifierUICollectionViewCell 无法出列同类视图:带有标识符的 UICollectionElementKindCell
【发布时间】:2020-07-05 17:51:05
【问题描述】:

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

线程 1:异常:“无法使类型视图出列:带有标识符 CHALLENGECELL 的 UICollectionElementKindCell - 必须为标识符注册 nib 或类或连接情节提要中的原型单元”

我为此查看了其他解决方案,但没有一个产生结果。

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

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 是我将可重用标识符分配给集合视图单元格的情节提要。

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell


    【解决方案1】:

    您必须在viewDidLoadcellForItemcellForItem 中出列的单元格。

    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")
    

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多