3

I have UICollectionView with horizontal scrolling and paging. When I scroll to next or previous page for the first time or change scrolling direction from left to right, value of indexPath.row in cellForItemAtIndexPath is changing by 3 not 1. Then it works properly. CollectionView works without problems in iOS 9. The problem occurs just in iOS 10. Thanks.

Jagveer Singh
  • 2,258
  • 19
  • 34
Roman Siro
  • 226
  • 1
  • 11
  • When you say "changing by 3" it's unclear what you mean. Do you mean you're never asked for some indexes at all? Or that you are asked for indexes in an order that surprises you? – Rob Napier Oct 05 '16 at 14:00
  • For better understanding I added video https://www.youtube.com/watch?v=BTaD_InGt6g&feature=youtu.be. The problem occurs just in iOS 10. – Roman Siro Oct 06 '16 at 14:19
  • You've probably miscalculated your page size. Without some code, it's not clear how to help you. You should trace your code and see exactly what datasource methods are called and with what values. I suspect you're being surprised by the new pre-fetching behaviors in collections views (collection views now request cells that are not necessarily visible yet). – Rob Napier Oct 06 '16 at 14:24
  • Thanks problem was with new prefetching feature . I had disabled it and problem disappeared. – Roman Siro Oct 06 '16 at 14:50
  • That strongly suggests you're incorrectly implementing your data source methods, and you should make sure to investigate that further. You should not make assumptions on what cells will be requested and when. Collection views do not promise that (and never did). – Rob Napier Oct 06 '16 at 15:34

2 Answers2

3

Problem was with new prefetching feature of UICollectionView. Disabling of prefetching solved my problem.

if #available(iOS 10.0, *) {collectionView.isPrefetchingEnabled = false}
Roman Siro
  • 226
  • 1
  • 11
0

Possible Case:

Since you're using paging in your collectionView, and as your declaration, 1 page contains 3 cells. So if you move 1 page, the index of cell will move by 3 because the index the taking from the cell at the left.

E.g. Cell > Page 1:[0..1..2] Page 2:[3..4..5]

KTang
  • 340
  • 1
  • 17
  • I use just use one cell for page . It works perfectly in iOS 9 . Problem in iOS 10 captured here: https://www.youtube.com/watch?v=BTaD_InGt6g&feature=youtu.be . – Roman Siro Oct 06 '16 at 14:24
  • As your video, it doesn't shows the collection view is presenting the correct cell to the corresponding page. That means I can't determine page 6 is showing number 6's cell. The cell may not right so that the "cellForItemAtIndexPath" can't return back correct value to you. By the way, I suggest you to use `collectionView.visibleCells` to find out which cell is displaying. And you can add `TAG` to those cells when you creating them by "cellForItemAtIndexPath". And use the tag number to show the row index. Or, it may have some logical error on the left/right button's method you wrote. – KTang Oct 13 '16 at 06:16