0

I did updated my data source, I dont use headers or footer in my collection view. At the first delegate its empty, then after the user add some data to the model, I am trying to add a new cell and section with :

    let indexPath = NSIndexPath(forItem: 0, inSection: (0) )
 collectionView.performBatchUpdates({

            self.collectionView.insertSections(NSIndexSet(index:0 ))
            self.collectionView.insertItemsAtIndexPaths([indexPath])

The crash has no explanation, but says something about array index - I don't know which array .

reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

on the first row where you insert the section. (data has 1 value in the array!)

I could see some people said its a bug ?

Curnelious
  • 1
  • 16
  • 76
  • 150
  • Does data have 1 value in the array before or after you try to show the collectionview? The order in which you add the item and show the view is important. – Putz1103 Jul 15 '16 at 17:01
  • What does your [cellForItemAtIndexPath](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDataSource_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDataSource/collectionView:cellForItemAtIndexPath:) implementation look like? My psychic powers tell me the problem is there. – i_am_jorf Jul 15 '16 at 17:01
  • 1
    "I could see some people said its a bug ?" I would like to quash that sort of thinking. The chances of your discovering a bug in Cocoa are vanishingly small. Learn to suspect _your_ code. – matt Jul 15 '16 at 17:02
  • You shouldn't need the `insertItemsAtIndexPaths` call. By inserting the section you are implicitly inserting all the cells that the datasource says are going to be in that section. – dan Jul 15 '16 at 17:11
  • Well, First, I updated the data model before everything. Regarding @dan I did tried to remove the insert cell, and I get the same error. the rest of my implementation is good, because if I use the reload data function it works great. This is the bug I was talking about : http://stackoverflow.com/questions/12611292/uicollectionview-assertion-failure – Curnelious Jul 15 '16 at 17:15
  • @vadian as I wrote 3 times, the obvious thing after 5 years is to update before you insert.... – Curnelious Jul 15 '16 at 17:16
  • The error message says, an object is going to be inserted at index **1** so logically the batch update block cannot cause the crash. – vadian Jul 15 '16 at 17:20

0 Answers0