Incorrect isSelected state of prefetched cells in UICollectionView after manually selecting/deselecting them

Originator:arkadiusz.holko
Number:rdar://32564327 Date Originated:05-Jun-2017 12:28 PM
Status:Open Resolved:
Product:iOS + SDK Product Version:10.3.2
Classification:Serious Bug Reproducible:Always
 
Summary:
Let’s assume we have a collection view with more cells than fit in its visible bounds and UICollectionView.isPrefetchingEnabled is true (which is a default).

When we call:
- selectItem(at:animated:scrollPosition:) or
- deselectItem(at:animated:)

on all cells, the following happens:

1. isSelected is correctly set on cells that are visible on screen.
2. isSelected is correctly set on cells during dequeueing.
3. isSelected isn’t set properly on prefetched cells, i.e. cells that were already dequeued (collectionView(_:cellForItemAt:) was called for them) but they aren’t visible on screen yet.

Steps to Reproduce:
1. Set up a collection view with 20 items, with 8 items visible at once on screen.
2. Call selectItem(at: indexPath, animated: false, scrollPosition: []) on all index paths in the collection view.
3. Scroll to the bottom of collection view.
4. Call deselectItem(at: indexPath, animated: false) on all index paths in the collection view.
5. Scroll collection view up.

Expected Results:
All cells should be deselected.

Actual Results:
Cells that are immediately out of visible bounds will be selected, even though we deselected them.

Version:
10.3.2

Notes:
Workaround:

Manually configure isSelected on cells in willDisplay:

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        cell.isSelected = collectionView.indexPathsForSelectedItems?.contains(indexPath) ?? false
    }

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!