I'd like to display an accessoryType in a UITableViewCell in which an UITableViewRowActionButton has been pressed. The accessoryType should also be displayed if the app relaunches. So I think I have to store something. Does someone have an idea how to do this?
-
I always use "NSUserDefaults.standardUserDefaults" to save the state of a cell. In "cellForRowAtIndexPath" just check if the key is set on true or false and that's it. Keep in mind, this will not work if you reuse a cell. In that case the solution is a bit more tricky but I guess you were asking for a single cell, right? – Armin Scheithauer May 14 '15 at 14:33
-
Thanks for your answer. How can I handle a delete of an UITableViewCell? – horst May 16 '15 at 20:04
-
Use the tableView function "editActionsForRowAtIndexPath" I would suggest you search on Stackoverflow how to implement that....there are many questions and ansers about that. – Armin Scheithauer May 17 '15 at 14:11
-
I know how to delete an UITableViewCell but how can I handle the NSUserDefaults? Don't do I have to delete / set an bool to false? – horst May 18 '15 at 11:32
-
You can just test if that key is "nil" or not. Otherwise you can do like you said, just use the "boolForKey" from NSUserDefaults and set it to true or false. There are many ways to achieve what you want to do. – Armin Scheithauer May 18 '15 at 11:52
-
I opened a new question. Hope you can help me: http://stackoverflow.com/questions/30350904/nsuserdefaults-store-bool. – horst May 20 '15 at 13:19
1 Answers
You need to learn how table views work. They have a data source that configures cells based on the saved state of the data model.
What you should do is set up your data model to save the fact that the user has pressed your cell button. Save that information to a file, and load it when the app launches.
Set up your cellForRowAtIndexPath to look at the saved information for that row and display an accessory in those cells where your saved information shows that the user has tapped your row action button.
In the IBAction method for your row action button, you can simply set the "the user has clicked the row action button for this row" flag and tell the table view to reload that indexPath. Your cellForRowAtIndexPath will then reload the cell with the accessory .

- 128,072
- 22
- 173
- 272