【发布时间】: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