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