0
votes

I have a collectionView with isPagingEnabled property true. My each CollectionView cell is same size of ViewController. I am using this collection view like pager.

Now I am trying to delete a cell at 'indexNumber' (Note: I am getting this indexNumber from somewhere else) using below code: -

let deletingIndexPath = IndexPath(item: indexNumber, section: 0)   // Line 1
self.collectionViewPager.deleteItems(at: [deletingIndexPath])      // Line 2
self.collectionViewPager.scrollToItem(at: deletingIndexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: true)           // Line 3

I am getting exception at line 2 as mentioned below: -

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (4) must be equal to the number of items contained in that section before the update (4), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

While I am able to insert item in collectioView with below code: -

let insertingIndexPath = IndexPath(item: self.numberOfPages - 1, section: 0)
self.collectionViewPager.insertItems(at: [insertingIndexPath])
self.collectionViewPager.scrollToItem(at: insertingIndexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: true)

Please help what I am missing in deleting item. Thanks in advance.

1
I got resolved above issue. This issue was due to I was trying to remove the item from my local array after Line 3. When I put logic to remove the item before Line 1. Got resolved. - Asif Raza

1 Answers

0
votes

I got resolved above issue. This issue was due to I was trying to remove the item from my local array after Line 3. When I put logic to remove the item before Line 1. Got resolved.