Strange behavior with UIScrollView and UICollectionView after iOS 16.2 update
I'm reviewing some code and I've been working on this all day and I'm dealing with I'm stuck on something that should probably be simple... Hey everyone, I'm running into an issue that's driving me crazy. I've recently updated my app to iOS 16.2, and I'm experiencing a peculiar scenario with a UIScrollView that contains a UICollectionView. Specifically, when I scroll the UIScrollView, the UICollectionView's cells momentarily display old data or even get mismatched with the wrong index paths. I've tried reloading the collection view data in `scrollViewDidScroll`, but it doesn't seem to resolve the scenario. Here's a snippet of my code: ```swift class MyViewController: UIViewController, UIScrollViewDelegate, UICollectionViewDataSource { @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var collectionView: UICollectionView! var items: [String] = [] override func viewDidLoad() { super.viewDidLoad() collectionView.dataSource = self loadItems() } func loadItems() { items = ["Item 1", "Item 2", "Item 3"] // Load your data here collectionView.reloadData() } func scrollViewDidScroll(_ scrollView: UIScrollView) { collectionView.reloadData() // This seems to cause issues } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return items.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) cell.textLabel.text = items[indexPath.item] // Updating the cell return cell } } ``` I also checked the console for any behavior messages and found a recurring warning: `UICollectionView's layout is invalidated while the view is transitioning`. This warning didn't appear before the update. I tried disabling animations and setting `collectionView.collectionViewLayout.invalidateLayout()` in `viewWillAppear`, but the scenario continues. Has anyone encountered similar behavior after upgrading to iOS 16.2? Any suggestions on how to resolve this cell data mismatch scenario when scrolling? How would you solve this? What's the best practice here? I'm coming from a different tech stack and learning Swift. Any pointers in the right direction? This is my first time working with Swift 3.9.