apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.
MIT License
1.35k stars 161 forks source link

All prepareForReuse() overrides skipped the superclass call, causing corrupted self-sizing layouts. #215

Closed andersio closed 3 years ago

andersio commented 3 years ago

Collection reusable view should call super.prepareForReuse().

The UICollectionViewLayout preferred layout attributes mechanism apparently holds internal states inside the cells/reusable views, which needs to be reset by super.prepareForReuse() before the cell is reused.

Since ASCollectionViewCell does not call super.prepareForResue(), those states are not being reset. This causes UIKit to consider a reused cell bound to a new index path has already been measured for preferred attributes, when it goes on screen. Therefore, reused cells are often stuck at the estimated size, where the whole preferred layout attributes lifecycle stops happening at all, while supposedly it should have been triggered every time the item goes on screen.

In order words, this causes issues with both UICollectionViewCompositionalLayout, UICollectionViewFlowLayout, and any custom layout that relies on preferred layout attributes on content self-sizing. Tested on both iOS 13.4 and 14.5.

UITableView cells should call super.prepareForReuse().

https://developer.apple.com/documentation/uikit/uitableviewcell/1623223-prepareforreuse

[...] If you override this method, you must be sure to invoke the superclass implementation.

apptekstudios commented 3 years ago

Thank you for picking this up, it actually explains a few weird bugs I couldn't figure out! 💯