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
}
}
私はnumberOfItemsInSection、plsチェックをアップロードします。ありがとうございます –
私は以前この問題に直面していました。また、あなたのtableviewデリゲート関数を投稿できますか?私の場合は、テーブルビューのセル内でコレクションビューのデータソースとデリゲートプロパティを設定していませんでした。 – Abhishek729