0
我在tableViewCell
的内部插入了collectionView
。 Tableview
包含类别列表和collectionView
包含所有产品。基于哪个表视图行被选中,我如何在collectionView
中获得不同数量的项目?我尝试存储选定的表格视图行,并使用它来定义要返回的项目数量,但它或者没有错误代码崩溃,告诉我值为nil
或只是不显示collectionView
中的任何clitems。任何帮助将不胜感激。感谢您的时间。正在更新收藏查看项目
下面是我的代码:
我的自定义表格视图单元格:
extension ExpandableCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let toReturn = categoryItems.count
return counter
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CustomCollectionViewCell
//What is this CustomCollectionCell? Create a CustomCell with SubClass of UICollectionViewCell
//Load images w.r.t IndexPath
print(self.selectedCategory.description)
let newArray = starbucksMenu[selectedCategory]
//cell.image.image = UIImage(named: (allItems[selectedCategory]?[indexPath.row])!)
cell.label.text = categoryItems[indexPath.row]
//cell.layer.borderWidth = 0.1
return cell
}
我的表视图的委托方法:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
word = indexPath.row
guard let cell = tableView.cellForRow(at: indexPath) as? ExpandableCell
else { return }
switch cell.isExpanded
{
case true:
self.expandedRows.remove(indexPath.row)
self.selectedCategory = ""
case false:
self.expandedRows.insert(indexPath.row)
}
self.selectedCategory = categories[indexPath.row]
print(self.selectedCategory)
//self.array = starbucksMenu[starbucksMenuCategories[indexPath.row]]!
//self.collectionView.reloadData()
cell.menuItems = allItems[selectedCategory]!
cell.categoryItems = allItems[selectedCategory]!
cell.isExpanded = !cell.isExpanded
self.itemsArray = allItems[selectedCategory]!
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
我试过很多东西,我试过将数组添加到数组中并返回计数(不显示任何内容)。我有一个必要的项目字典,所以我也尝试返回allItems [selectedCategory]?。count并且这总是返回一个错误,我相信selectedCategory没有值,一旦这被称为。
能否请你告诉你的代码藏汉? – Victor
@Victor我已经添加了我的代码,提前谢谢。 – Blackroche