Invalid supplementary view index path passed to layout

Originator:kellerbryan19
Number:rdar://48955657 Date Originated:3/16/19
Status:Open Resolved:No
Product:UIKit Product Version:iOS 12
Classification:Bug Reproducible:Yes
 
Area:
UIKit

Summary:
When an unsized supplementary view slides into view due to a section delete above it (via batch update), UICollectionView may pass preferred attributes for the supplementary view to the layout with the supplementary view's original, pre-batch-update index path set on the attributes. It's unclear how the layout should handle this, since that index path is now invalid / possibly out of bounds. 

In the attached sample project, there are three sections, each with headers and items. If section 0 is scrolled off screen, section 1 is visible, and section 2 is below the bottom edge, remove section 0 results in the following facts:
- The collection view goes from 3 sections to 2
- The unsized header that was in the 3rd section (section index 2) before the batch update will now have section index equal to 1
- shouldInvalidateLayout(forPreferredLayoutAttributes:withOriginalAttributes:) is invoked for a supplementary view with section index equal to 2 - section 2 is not valid, this should be section 1 at this point

This issue is very similar to rdar://41492798. The only reason flow layout is fine is because it doesn't support self-sizing headers.

Steps to Reproduce:
1. Open sample project
2. Deploy to iPhone 8 simulator (that screen size makes it easier to get things lined up correctly)
3. Scroll down so that section 0's header is completely off-screen, section 1's header is on screen, and section 2's header is still off screen below the bottom edge. Make sure to not scroll down to far, since if section 2's header comes onto screen, it will receive preferred attributes and prevent this issue. *This issue only happens when the supplementary view sliding onto screen has not been sized yet.*
3. Tap anywhere to execute the batch update
4. Observe issue

Expected Results:
UICollectionView should never ask for information about an invalid / non-existent / old section index

Actual Results:
UICollectionView asks for information about an invalid / non-existent / old section index

Version/Build:
iOS 12

Configuration:
iPhone 8 screen size makes it easier to repro

Sample project: https://drive.google.com/file/d/1Q6fSCwWax93E7vumuy8N3W9V-bHptW1n/view?usp=sharing

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!