首页 > 解决方案 > Сould not dequeue a view of kind

问题描述

错误:无法使类型视图出列:带有标识符 currencyCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元

我不明白错误是什么。在情节提要中,两个 collectionView 都有 ReuseIdentifier

还需要什么?

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource  {
  // MARK: IBOutlets
    @IBOutlet var currencyCollectionView: UICollectionView!
    @IBOutlet var categoryCollectionView: UICollectionView!
    
    let category = ["Popular", "Sports", "Insider", "Auto", "Science"]
    
  // MARK: Private properties
    private var currencyList: [Currency] = []
  
    override func viewDidLoad() {
        super.viewDidLoad()
      
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == categoryCollectionView {
            return category.count
        }
        return currencyList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "currencyCell", for: indexPath) as! CurrencyCollectionViewCell
        cell.layer.cornerRadius = 10
        cell.label.text = currencyList[indexPath.row].name
        cell.value.text = currencyList[indexPath.row].value
        
        if collectionView == categoryCollectionView {
            let categoryCell = collectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell", for: indexPath) as! CategoryCollectionViewCell
            categoryCell.categoryLabel.text = category[indexPath.row]
            categoryCell.backgroundColor = .blue
            return categoryCell
        }

        return cell
    }
}


标签: swiftxcodeuicollectionview

解决方案


设置 Collectionview 单元格标识符

如果您使用 cellectionview 本身的单元格,则需要提供单元格标识符,如屏幕截图所示,并且它在代码中也必须相同,如下所示

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AppListCell", for: indexPath) as! CurrencyCollectionViewCell

推荐阅读