【发布时间】:2020-02-17 04:14:42
【问题描述】:
我的代码在 UITableViewCell 中有一个 UICollectionView。我有两组不同的数组,分别命名为 TopBrandArray 和 TopCategoryArray——它们显示在 collectionView 内 Table 的两个不同单元格中。
委托和数据源已连接到主情节提要中的 HomeViewController。此外,还给出了单元格的名称。
编译程序时没有错误,但在运行代码时得到一个期望。
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 TopCategoryCollectionID 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格
当我试图通过只显示 tableviewcell 的一个单元格来运行时,代码可以正常工作。但是,如果我添加更多单元格,则会弹出上述错误。在这里,我添加代码:
@IBOutlet weak var homePageTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.homePageTable.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "TopCategoryID", for: indexPath) as! TopCategoriesTableViewCell
cell.tablecollection1.reloadData()
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "BrandTableID", for: indexPath) as! BrandsTableViewCell
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 162
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0{
return topCategoryArray.count
}
else{
return topBrandArray.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopCategoryCollectionID", for: indexPath) as? TopCategoriesCollectionViewCell{
cell.cat_image.image = UIImage(named: topCategoryArray[indexPath.row].image)
print(topCategoryArray[indexPath.row].name)
cell.cat_value.text = topCategoryArray[indexPath.row].name
return cell
}
else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BrandsCollectionID", for: indexPath) as! BrandsCollectionViewCell
cell.layer.borderWidth = 1.5
cell.layer.borderColor = UIColor.gray.cgColor
cell.layer.cornerRadius = 4
cell.brandimage.image = UIImage(named: topBrandArray[indexPath.row].image)
return cell
}
}
}
由于它要求为标识符注册一个 nib 或一个类,我不知道我应该在代码中的哪个位置编写代码,因为当我在 viewDidload() 中编写代码时,弹出一个错误,显示“不明确的引用会员collectionView(_:numberOfItemsInSection:)"
【问题讨论】:
标签: ios uitableview exception uicollectionview swift5