2017-05-09 6 views
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は値を持たないと思います。

+0

あなたはあなたのコードのaswellを示していただけますか? – Victor

+1

@Victor私は自分のコードを追加しました、ありがとうございます。 – Blackroche

答えて

0

beginUpdateメソッド()とEndUpdateの(間に適切な操作でコレクションビュー項目のループのために作る)

関連する問題