【发布时间】:2016-10-25 10:29:04
【问题描述】:
多个自定义 TableviewCell 上的多个自定义 UICollectionView
我想要的: 多个 TableviewCell 上的多个自定义 UICollectionView。
 这些因部分而异。
我遵循了这个教程:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
但我无法弄清楚为什么会出现问题,
错误显示,
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 ProductCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格”
我从 cellForRowAt 注册了 Nib 文件, 当然,我在 Nib 文件上添加了标识符。
来自TableView的方法,像这样
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section{
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: product, for: indexPath) as! ProductTableViewCell
cell.collectionView.register(UINib(nibName: "ProductCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ProductCell")
cell.collectionView.viewWithTag(0)
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ShowDataTableViewCell
cell.collectionView.register(UINib(nibName: "ShowDataCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
cell.collectionView.viewWithTag(1)
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ShowDataTableViewCell
cell.collectionView.register(UINib(nibName: "ShowDataCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
cell.collectionView.viewWithTag(99)
return cell
}
}
这不起作用,我也尝试从 TableViewCell 注册 nib 文件,
但它也不起作用。
还是 numberOfItemsInSection 方法出现问题?
extension TableChildFoodViewController : UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
print("tag: \(collectionView.tag)")
switch collectionView.tag{
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as! ProductCollectionViewCell
return cell
case 1:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! ShowDataCollectionViewCell
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! ShowDataCollectionViewCell
return cell
}
}
}
如果可能的话,你能给我建议吗?
【问题讨论】:
-
你确定你有名为“ProductCell”的标识符。请检查是否有任何拼写错误。
-
是的,我确实在 nib 文件中添加了该标识符
-
尝试交换这两行。 (出队前注册)
-
@Nitya 你是什么意思?怎么换?哪个代码?
-
下一行的产品是什么?让 cell = tableView.dequeueReusableCell(withIdentifier: product, for: indexPath) as! ProductTableViewCell 不应该是“产品”吗?
标签: ios swift uitableview uicollectionview swift3