UICollectionView ignores taps and highlights wrong cell during reload

Originator:lap.felix
Number:rdar://47031301 Date Originated:January 3 2019, 3:36 PM
Status:Open Resolved:
Product:iOS + SDK Product Version:12.1
Classification:UI/Usability Reproducible:Always
 
Area:
UIKit

Summary:
When a UICollectionView cell is tapped shortly after -[UICollectionView reloadData] is called, the wrong cell gets highlighted and no cell ends up being selected.

Steps to Reproduce:
It's reasonably easy to reproduce by tapping a cell right after reloadData is called but it's easier to reproduce when UICollectionView is reloaded every 0.1-0.2 seconds:

1. Reload UICollectionView frequently
2. Tap a cell

Expected Results:
The tapped cell should be highlighted and -[UICollectionViewDelegate collectionView:didSelectItemAtIndexPath:] should be called with the right indexPath.

Actual Results:
A seemingly random cell gets highlighted (the 1st cell always gets reused in the 1st position so the behaviour can't be reproduced with that one) and the tap is cancelled so didSelectItemAtIndexPath isn't called.

It seems like -[UICollectionView touchesBegan:withEvent:] has a touch with the right position but the touch's view is a cell that isn't correctly placed anymore. I have a pretty hacky workaround (which can be enabled in the sample project (ViewController.m:33)) that works when the cell doesn't have buttons as subviews by detecting if the touch's location is within its view and if it's not, we go find the view and override it on the touch. Obviously that's not ideal.

Version/Build:
I could reproduce in iOS 9.0, iOS 10.3.1, iOS 11.4 and iOS 12.1.1

Comments

Sample project: https://github.com/lapfelix/CollectionViewReloadIssue

By lap.felix at Jan. 3, 2019, 9:02 p.m. (reply...)

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!