3

I learned the UICollectionView inside UITableViewCell from this tutorial.

It works perfectly only when all of the collection views have the same numberOfItemsInSection, so it can scroll and won't cause Index out of range error, but if the collection view cells numberOfItemsInSection are different, when I scroll the collection view it crashes due to Index out of range.

I found the reason that when I scroll the tableview, the collection view cell index path updated according to the bottom one, but the collection view I scrolled is top one so it does't remember the numberOfItemsInSection, so it will crash due to Index out of range.

ps: it is scrollable, but only when that table cell at the bottom, because its numberOfItemsInSection is correspond to that cell and won't cause Index out of range.

This is the numberOfItemsInSection of My CollectionView, i actually have two collection view but i think this is not the problem

    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
    }

This is the cellForItemAt indexPath of my CollectionView:

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
        }

    }
Bista
  • 7,869
  • 3
  • 27
  • 55
Yang Xu
  • 41
  • 1
  • 7
  • i upload my numberOfItemsInSection, pls check. Thank you – Yang Xu Nov 07 '16 at 08:59
  • I had faced this problem earlier. Could you also post your tableview delegate functions? In my case I had not set the datasource and delegate property for the collection view inside the table view cell. – Abhishek729 Nov 07 '16 at 12:28

1 Answers1

1

The problem is probaby that you are returning the same number every time for numberOfItemsInSection what you want to do is return the number of items in each array. From the link you sent they do this:

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

    return model[collectionView.tag].count
}

The other possibility is that you are returning the wrong array when cellForItemAtIndexPath please check that you are using the tag attribute correctly

So they get the count of each array within the model. Without seeing any of your code its very difficult to actually see where your error is coming from.

yawnobleix
  • 1,204
  • 10
  • 21
  • i print the numberOfItemsInSection and its not same, when i scroll the tableview and the lowest table cell show, it will refresh and cover the numberOfItemsInSection of last one. For example, the table cell i scroll have a collection view of 3 items, but when i scroll tableview lowest cell only have 1 colletion view item, and when i scroll it shows Index out of range. But when that 3items cell is the lowest one it can scroll successful. – Yang Xu Nov 07 '16 at 08:45
  • i post the numberOfItemsInSection part. self.itemimages_index is the index i get from tableview cell – Yang Xu Nov 07 '16 at 08:54
  • you should also post cellForItemAtIndexPath – yawnobleix Nov 07 '16 at 09:01
  • to be honest buddy i don't see what your mistake is, maybe try to debug it with just one collectionview and see if it works then. Your error is likely coming coming because for some reason numberOfItemsInSection returns an int thats greater than the size of your array. Maybe check at which index the error occurs and compare it to your array size – yawnobleix Nov 07 '16 at 13:25