UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Why is a call to reloadItems(at:) stopping/breaking call to reloadData() in Collection View?

Forums > Swift

Hi everyone,

In my collection view, I have implemented a "selection" feature that changes the background color of a cell on tap (without actually selecting the cell) by calling reloadItems(at:) that cell's specific index path, and changing the background color in cellForItemAt depending on whether that cell's index was tapped. Separately, I have an on-screen switch that toggles whether certain indicators are hidden in all cells, by calling reloadData() and loading in a different appearance from cellForItemAt depending on which option is selected.

Separately, these actions work as intended. However, if the cell is selected just before the switch is toggled, the indicators do not disappear, suggesting that the call to reloadItems(at:) stopped or disrupted the call to reloadData(). If I scroll to new cells, those cells appear with the indicators hidden correctly, and the original cells are set correctly when I scroll back (once the cells have been reloaded, they are correct). This malfunction does not happen in the reverse order, when the switch is toggled just before a cell is tapped.

I have put every function associated with the cell selection and indicator switch onto the main thread with DispatchQueue.main.async, hoping that that would cause the calls to reloadItems(at:) and reloadData() to be forced to run in the order in which they were called, without overlapping. However, this did not work.

Why does the call to reloadItems(at:) disrupt the call to reloadData(), and is there a way to fix this problem? Is there a secret order of operations for reloading data in a collection view? Are there certain conditions that a call to reload must meet in order to fully reload that collection's cells?

Any help would be greatly appreciated.

Thank you, Thomas

3      

Generally you should not be using the reloadData. This is kind of the "nuclear" option, that clears the collection view and loads everything back again.

Based on the behaviour you described, it looks like not all the state is correctly saved to the model and therefore with reloadData you loose some of the modifications to the collection view.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.