UITableView may have negative contentOffset after reloading rows

Originator:demid.itsk
Number:rdar://48977017 Date Originated:2019-03-18
Status: Resolved:
Product:iOS + SDK (UIKit) Product Version:12.1
Classification:Bug Reproducible:Always
 
Area:
UIKit

Summary:
When reloading UITableView rows after changing their height, UITableView may end up having a negative contentOffset, which results in a white (background color) area at the top.
It depends on cells height and screen size.

Steps to Reproduce:
After downloading the attached project (you can also get it from here: https://github.com/DMAPro/iOSCollapseCellContentOffsetBugDemo):

1. Pick iPhone SE Simulator (or a device) with iOS 12.1 (or 11.x) and launch the app. The bug can be reproduced (as it seems) on any device, but it depends on the combination of cells height and screen size, so cells height in this project is tuned for iPhone SE.
2. Expand all the cells (by tapping on them).
3. Collapse cells in the following sequence: 3, 1, 2 (without touching cell #0 or scrolling the screen).

Expected Results:
We end up with the top cell expanded, and other cells collapsed, top cell is at the top of the screen.

Actual Results:
There’s a blank area between UITableView’s top and the top cell. It disappears once you touch the screen.

Note:
There’s a video in the archive that demonstrates the issue.

Version/Build:
iOS 11.x / 12.x. Can't reproduce it on iOS 10.x.

Configuration:
The attached project is tuned for iPhone SE, but it should be possible to reproduce on any iPhone (maybe iPad too), you'd just need to pick cell heights and the order in which they should be closed.

Workaround: 
First option: set "Estimated" row height to "Automatic" in the Storyboard (that's what differs the "bug" and "no bug" versions)
Second option: implement proper `estimatedHeightForRow:atIndexPath:`.

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!