-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Row with SwitchAccessory loses its state after scrolling the table #135
Comments
Hi @n3d1117 , this is likely due to cell reuse/dequing/prepareForReuse. Although you are initiating Row with an accessory "switchToggle(value:)", since this value is a struct rather than var, it doesn't change. So your row is still referencing an old boolean value. To Update to support with current Static implementation: If you're interested in providing this support, perhaps you might file a PR to support updating self's accessoryView's value given it's a switch? As that seems like a nice to have functionality other folks would appreciate. |
Hi @dmiluski ! Thanks for your answer.
Would be the best place to update the row/accessory for this particular type of row? I tried hooking
This works, although it feels bad to reload the row just because switch value changes. I'm doing something like this in ViewController:
In viewDidLoad:
However this is pretty bad, as it involves knowing the index of each row with a switch, as well as extracting every one of these rows into their own var to reassign it later. I'm sure there's a better way, and I would be glad if you could point me to the right direction. Thanks again! |
From what I understand this Repo began as a way to quickly render/show a static set of cells. It did evolve to allow for mutation, but it's not quite beneficial for that just yet in terms of something like a form without reloading the section. It was only recently that a switch support was added, and consideration into this use case may have been overlooked. Funny enough, just looking through my use case, our action is handled with an API call, who's latency covers up the Row replacement that you mentioned would be glitchy when done and replaced locally. |
Hm.. wondering if there's a way to add a row mutating closure which could update your model and play friendly with tableView refresh/reload. |
What's available: Potentially:
This way, you don't need to refresh the full table. And the only item that is mutated is in context, which is only re-applied if table is reloaded, or dequeue for reconfiguration. So far just a theory. Would need to test it out. |
It worked! Somehow I tested the same thing yesterday, but using Bool directly (which is a value type and not a reference type), so it didn't work. Using a class did the trick!
In ViewController:
Works great! The only downside is that SwitchCell now needs to be aware of what property is it toggling (i.e. Do you think it can be improved in any way? Again, thanks a lot for your help @dmiluski |
Looks very cool. Improvements: As for separate Cell vs Accessory, I think they both serve their own purposes. I think in this particular case the cell may be preferable over the accessory given the requirement of mutability you mention. For other's implementations, it's very likely that these accessories/rows are being regenerated after an API call in order to keep in sync. |
Hi again @dmiluski! Using KeyPaths I came up with this:
And then declare the row like this:
It seems to work! The cell now automatically updates the model when switch value changes. What do you think? |
Nice solution, @n3d1117! I think we've been struggling with the |
Second @eliperkins 's comment, very cool. I'd be happy moving forward with this. @eliperkins any thoughts on constant keys for context in this case as it requires a pair to exist? Given the pain of the context api. |
Hello!
I am using a Row with UISwitch as seen in the example project here.
However when scrolling down the table and then up again, the switch loses its
on
state.Tested straight from example project:
Is this due to cell reusing? How can I preserve switch state on scroll?
Thanks
EDIT: This is the way I'm using it. Am I doing something wrong?
var value: Bool = false
The text was updated successfully, but these errors were encountered: