swift - С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
}
}