Clark
Clark

Reputation: 406

Saving Static Tableview Cell Check Marks using NSUserDefaults

**New to iOS dev and programming in general. Patience please.

I have looked everywhere for something that I can understand and implement that will allow me to save and retrieve check marks for static tableview cells.

I have tried:
saving checkmark accessory value in nsuserdefaults and then retreving them
how to save the state in userdefaults of accessory checkmark-iphone
and countless others.

It is probably my lack of knowledge that is causing the problems.

I have a tableview with static cells. I have implemented the check mark for multiselection, per Apple's instructions as follows:

- (void)tableView:(UITableView *)theTableView
didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {

    [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
    UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;

    }
}

This works fine for selecting and unselecting multiple rows, which then display the checkmark.

How do I save those checkmarks to user defaults so that they reload everytime for that tableview?

When I try to set the object for the NSUserDefaults, I get an error that says "Implicit conversion of NSInteger to 'id' is disallowed with ARC" and "Incompatible integer to pointer conversion sending NSInteger to parameter type of 'id'".

Any help is much appreciated, even if it is just pointing me to a resource that is understandable for a rookie.

Thank you!

Upvotes: 1

Views: 552

Answers (2)

Carl Veazey
Carl Veazey

Reputation: 18363

In general, you should be looking to modify the underlying data represented by the table view, and then persist that, rather than modify and persist the state of the table view's representation of the data.

However, if there's no clear way to do that, then to fix your current error you must remember that NSUserDefaults only stores objects, not primitive types. So to convert an integer expression to an NSNumber box it with @(...expr...), and then get the integer value for the object that you retrieve from NSUserDefaults by calling the integerValue method on it. You don't show any of the code actually causing the error, though, so that's as far as I can comment.

Upvotes: 0

user189804
user189804

Reputation:

I think you might be approaching this from the wrong direction. Your table view is data driven so you should be modifying your data, not your views, in response to cell selection. Then you cause your views to be updated either by doing [tableView reloadData] or for more fine grained control (i.e. modifying one at a time) using reloadRowsAtIndexPaths:withRowAnimation:. The idea is that you update the underlying data and you cause iOS to update the cell, which you configure according to that data, in cellForRowAtIndexPath:.

Upvotes: 1

Related Questions