【问题标题】:Сould not dequeue a view of kindСould not dequeue a view of kind
【发布时间】:2021-01-07 23:11:07
【问题描述】:

错误:无法使类型视图出列:带有标识符 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
    }
}


【问题讨论】:

  • 网点已连接?
  • 是的,在 CollectionViewCell 中
  • 我的意思是集合视图本身

标签: swift xcode uicollectionview


【解决方案1】:

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

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

【讨论】:

  • 我写了所有的东西都在情节提要中说明了。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2017-12-03
相关资源
最近更新 更多