Is the easiest way to hold tableview state?

I currently have a simple tabular view with 7 cells. When a row is selected, I add a checkmark to the cell and vice versa, when the selected cell is selected, it is not checked.

However, when I return from the table and return, all the checkmarks have disappeared.

I was wondering, what is the simplest solution in iOS to keep the state of checked cells so that when you return to viewing the table, the checkboxes still exist for the marked cells?

+4
source share
5 answers

I read a few posts a while ago on this subject. It seems to me that I can’t find what helped me, but here are some of them that I thought were good:

+2
source

The easiest way to do this is to save these checked states somewhere in the table view. This assumes that your table view remains somewhere in memory when you leave the screen. If the table view is not stored in memory (how do you allocate it when you decide to move there and free when you exit), then it would probably be easier to save it somewhere in the application delegate, since we know that it remains in memory during the term of the application

+1
source

What you want to do is your data model, which provides the data from which each cell is configured, add a flag (if you do not already have it), which will be used to indicate this case. Make sure you check the box in the tableView:cellForRowAtIndexPath: based on your data model for that particular cell.

When you get back to it, if your data source has not changed, when the tableview sets up its cells again before displaying the tableview, it will use this data to display a checkmark based on the settings in your data source.

+1
source

save table state in sqlite

0
source

save state in NSUserDefaults.

in your method didSelectRowAtIndexPath tableView delegate the default value for the user:

 NSUInteger defaultsCheckedRow=[[NSUserDefaults standardUserDefaults] integerForKey:@"rowForCheckedCellInNameOfTableViewController"]; if (defaultsCheckedRow==indexPath.row){ // deselect the row defaultsCheckedRow=defaultsCheckedRow*-1; }else{ defaultsCheckedRow=indexPath.row; } [[NSUserDefaults standardUserDefaults] setInteger: defaultsCheckedRow forKey: "rowForCheckedCellInNameOfTableViewController"] [[NSUserDefaults standardUserDefaults] synchronize]; [self.tableView reloadData]; 

Then, in your cellForRowAtIndexPath: Data source delegate method, you can do something according to:

 NSUInteger defaultsCheckedRow=[[NSUserDefaults standardUserDefaults] integerForKey:@"rowForCheckedCellInNameOfTableViewController"]; if (indexPath.row==defaultsCheckedRow){ cell.accessoryType=UITableViewCellAccessoryCheckmark; }else{ cell.accessoryType=UITableViewCellAccessoryNone; } 
0
source

All Articles