1

I have a collectionView which tries to implement infinite horizontal scrolling. The idea is that after a certain offset.x, I insert new items in performBatchUpdates and this is how I get my new cells.

I get the following exception inserting an array on indexPaths:

'NSInternalInconsistencyException', reason: 'attempt to insert section 9223372036854775807 but there are only 1 sections after the update'

Here is the code when I add the indexPaths:

func scrollRight() {
    var indexPaths = [IndexPath()]
    for i in 0..<7 {
        indexPaths.append(IndexPath(item: i, section: 0))
    }
    collectionView.performBatchUpdates({
        numberOfItems += 7
        collectionView.insertItems(at: indexPaths)
        for i in 0..<7 {
            days[i] -= 7
        }
    }, completion: nil)
    indexPaths.removeAll()
    for i in 7..<14 {
        indexPaths.append(IndexPath(item: i, section: 0))
    }
    collectionView.performBatchUpdates({
        numberOfItems -= 7
        collectionView.deleteItems(at: indexPaths)
    }, completion: nil)
}

That is triggered in ScrollViewDidEndDragging, any ideas why would it do that? There is only one section; also I update my dataSource for the numberOfItemsInSection before I get the indices inserted. Thanks for any help!

NicolasElPapu
  • 1,285
  • 1
  • 7
  • 22
grisVladko
  • 33
  • 1
  • 6
  • Don't update collection view directly, but instead update your datamodel (add more items) when you scroll to the right, then reload your collection view. – koen Jun 21 '20 at 20:36
  • that works, thanks, maybe you know what is the best approach to get a horizontal infinite scrolling collectionView? because with that approach i cannot add items when i scroll to the left, and it's also need. if you want post your comment as an answer and ill accept it – grisVladko Jun 22 '20 at 06:28
  • Maybe this will help you: https://stackoverflow.com/questions/29298091/infinitely-scrolling-in-both-directions-of-uicollectionview-with-sections – koen Jun 22 '20 at 09:52

0 Answers0