我正在尝试将UICollectionView放在UITableViewCell中,但我想我做错了...所以这是我的代码:
CartMenuCell是UITableViewCell
extension PanierMenuCell : UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return listProduit.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellIdentifier = "PanierCollectionCell"
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? PanierCollectionCell else {
fatalError("erreur de conversion !!")
}
cell.ONom.text = listProduit[indexPath.row].nomProduct
cell.OQtt.text = "x\(listProduit[indexPath.row].nbProduct)"
cell.OImage.image = UIImage(named: "test")
cell.backgroundColor = .gray
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.cornerRadius = 5
return cell
}
}
这是我的类调用UITableViewCell:
extension PanierVC : UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listPanierProduct.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "PanierMenuCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PanierMenuCell else {
fatalError("erreur de conversion !!")
}
let produit = listPanierProduct[indexPath.row].product as! Menu
let listProduit = produit.getListProducts()
cell.listProduit = listProduit
cell.OTotal.text = "Total: \(produit.prix)0€"
cell.ONomMenu.text = "Menu : \(produit.nom)"
cell.backgroundColor = .gray
return cell
}
所以这就是问题所在:当我在内部使用带有collectionView的几个单元而不滚动tableview时,这很有效,问题是我需要向下滚动tableView(tableview重新加载单元格),这会导致错误:“线程1:致命错误:索引超出范围”
cell.ONom.text = listProduit[indexPath.row].nomProduct
该项目内置UICollectionView是混合在一起的王者,我的意思是tableviewcell将采取一些假设在其他tableviewcell中的项目,就像cell.listproduit混合一样...不知道我有多清楚,但是如果需要,我可以解释更多。
关于如何解决这个问题的任何想法?非常感谢
您需要在更改数据源后重新加载UICollectionView。这将更新集合视图中的项目数。
所以在这一行之后:
cell.listProduit = listProduit
添加此行(假设您有一个引用您的UICollectionView的属性):
cell.collectionView.reloadData()