2

UITableViewCell from this tutorialの中でUICollectionViewを学習しました。uitableviewcell内のuicollectionviewは、インデックスが範囲外になったためクラッシュする

それがスクロールすることができますし、範囲エラー外のインデックスを引き起こすが、私はコレクションビューをスクロールするとコレクションビュー細胞numberOfItemsInSectionが、異なっている場合ではないだろうので、それは、コレクションビューのすべてが同じnumberOfItemsInSectionを持っている場合にのみ、完璧に動作します範囲外のインデックスが原因でクラッシュします。

私がテーブルビューをスクロールすると、コレクションビューのセルインデックスのパスが下位のものに従って更新されましたが、スクロールしたコレクションビューは先頭になりますので、numberOfItemsInSectionを覚えていないので、クラッシュしますインデックスが範囲外です。

ps:スクロール可能ですが、numberOfItemsInSectionがそのセルに対応しており、Index out of rangeを発生させないため、その表のセルが最下部にあるときのみです。これは私のCollectionViewのnumberOfItemsInSectionある

、私は、実際には2つのコレクションビューを持っているが、私は、これはこれは私のCollectionViewのcellForItemAt indexPathある

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     var numberOfItemsInSection: Int = 0 

     if collectionView == self.collectionview_categorymenu { 
      let categories = self.json_menucategory["menu_categories"].arrayValue 
      numberOfItemsInSection = categories.count 
     } else { 
      let menuitems = self.json_menucategory["menu_items"].arrayValue 
      let item_data = menuitems[self.itemimages_index].dictionaryValue 
      let menu_item_images = item_data["menu_item_images"]?.arrayValue 
      numberOfItemsInSection = (menu_item_images?.count)! 
      print(numberOfItemsInSection) 
     } 

     return numberOfItemsInSection 
    } 

問題ではないと思う。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if collectionView == self.collectionview_categorymenu { 

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryMenuCell", for: indexPath) as! CategoryMenuCell 

      return cell 
     } else { 

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemImagesCell", for: indexPath) as! ItemImagesCell 

      let menuitems = self.json_menucategory["menu_items"].arrayValue 

      let item_data = menuitems[self.itemimages_index].dictionaryValue 

      let menu_item_images = item_data["menu_item_images"]?.arrayValue 


      print(menu_item_images!) 
      let itemimage_data = menu_item_images?[indexPath.item].dictionaryValue 
      print("===\(indexPath.item)===") 

      let itemimage_path = itemimage_data?["image"]?.stringValue 
      let itemimage_detail = itemimage_data?["detail"]?.stringValue 


      if let itemimage = cell.imageview_itemimage { 
       let image_url = itemimage_path?.decodeUrl() 
       URLSession.shared.dataTask(with: NSURL(string: image_url!) as! URL, completionHandler: { (data, response, error) -> Void in 

        if error != nil { 
         print("error") 
         return 
        } 
        DispatchQueue.main.async(execute: {() -> Void 
         in 

         itemimage.image = UIImage(data: data!)! 
        }) 
       }).resume() 
      } 

      if let description = cell.textview_description { 
       description.text = itemimage_detail 
      } 

      if let view = cell.view_description { 
       view.isHidden = true 
      } 

      return cell 
     } 

    } 
+0

私はnumberOfItemsInSection、plsチェックをアップロードします。ありがとうございます –

+0

私は以前この問題に直面していました。また、あなたのtableviewデリゲート関数を投稿できますか?私の場合は、テーブルビューのセル内でコレクションビューのデータソースとデリゲートプロパティを設定していませんでした。 – Abhishek729

答えて

1

問題numberOfItemsInSectionのために毎回同じ番号を返すことになっているかどうかは、各配列の項目数を返すことです。あなたは、彼らがこれを行う送信されたリンクから:

func collectionView(collectionView: UICollectionView, 
    numberOfItemsInSection section: Int) -> Int { 

    return model[collectionView.tag].count 
} 

他の可能性がcellForItemAtIndexPathが正しく

タグ属性を使用していることを確認してくださいので、彼らはそれぞれのカウントを取得するときに間違った配列を返すされていることですモデル内の配列。あなたのコードのいずれかを見ることなく、あなたのエラーがどこから来ているかを実際に見ることは非常に困難です。

+0

numberOfItemsInSectionとそれが同じではなく、私がtableviewと最も低い表のセルをスクロールするときに、それが更新され、最後の1つのnumberOfItemsInSectionをカバーするときと同じではありません。 たとえば、テーブルセルのiスクロールは3つのアイテムのコレクションビューを持ちますが、スクロールテーブルビューの最下位セルには1つのcolletionビューアイテムしかなく、スクロールするとインデックスが範囲外です。 しかし、その3つのセルが最低のものであれば、スクロールできます。 –

+0

あなたのコードを投稿して見せることができますか? – yawnobleix

+0

私はnumberOfItemsInSectionの部分を投稿します。 self.itemimages_indexはテーブルビューのセルから取得するインデックスです –

関連する問題