2

**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!

Community
  • 1
  • 1
Clark
  • 416
  • 3
  • 10

2 Answers2

1

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:.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
  • Thank you, Adam! Would I reload the tableView data within the viewDidLoad method of that table view controller? Please excuse the question if it is obvious to others. – Clark Nov 04 '13 at 02:12
  • You can do [tableView reloadData] at any time but I don't think you will need to call it in viewDidLoad. If the data is unchanged then creating and adding the tableView to your hierarchy will cause it to go through the process of counting sections and rows, asking for row height and cell contents etc. If data only changes with cell interaction - such as in didSelectRowAtIndexPath: - then only do reloadData at that point. Calling it more often probably won't hurt but it won't help either. – Adam Eberbach Nov 04 '13 at 02:34
1

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.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81