由于索引超出范围,uitableviewcell内部的uicollectionview将崩溃

时间:2016-11-07 08:30:50

标签: ios swift uitableview uicollectionview indexoutofrangeexception

我在UITableViewCell from this tutorial中学习了UICollectionView。

仅当所有集合视图具有相同的numberOfItemsInSection时,它才能正常工作,因此它可以滚动并且不会导致Index超出范围错误,但是如果集合视图单元格numberOfItemsInSection不同,当我滚动集合视图时,由于Index超出范围而崩溃。

我发现原因是当我滚动tableview时,集合视图单元格索引路径根据底部视图路径更新,但我滚动的集合视图是最顶层的,所以它不记得numberOfItemsInSection,所以由于Index超出范围,它会崩溃。

ps:它是可滚动的,但仅限于底部的表格单元格,因为其numberOfItemsInSection对应于该单元格而不会导致Index out of range

这是我的CollectionView的numberOfItemsInSection,我实际上有两个集合视图,但我认为这不是问题

    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
    }

这是我的CollectionView的cellForItemAt indexPath

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
        }

    }

1 个答案:

答案 0 :(得分:1)

问题是probaby你每次为numberOfItemsInSection返回相同的数字你想要做的是返回每个数组中的项目数。从您发送的链接中,他们执行此操作:

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

    return model[collectionView.tag].count
}

另一种可能是你返回错误的数组,当cellForItemAtIndexPath请检查你是否正确使用了tag属性

因此,他们得到模型中每个数组的计数。在没有看到任何代码的情况下,很难真正看到错误的来源。