I have a list app where users hit the + button and enter in an item that they want to appear in the list and hit save. The table is saved with core data. The only problem is when the cell is taped I want a checkmark to be displayed. Ive enabled multiple selection with
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
I would like the checkmarks to be persisted in the cell after the user exits. I have created an attribute in my entity called “checks” and gave it the type of boolean but I dont know how to make it where if you hit a row then a check appears and is persisted. Any help would be greatly appreciated. Thanks
This is how I do it. One notable point: CoreData does not store booleans, so any property labeled “boolean” is actually of type
NSNumber. You’ve got to remember to convert back and forth when dealing with CoreData and boolean values.I have my
UITableViewControllerset as the the delegate for theNSFetchedResultsController, so the changes I made to the managed objects in the query ^^^ will cause the following two methods to be run.Here’s how everything ties together
controllerDidChangeContentmethod on its delegate.controllerDidChangeContentmethod just reloads all the data in the table viewAnd just so you don’t get confused, I initially used a generic
NSMangagedObjectto store row state, which is why the first method I posted says,[selectedObject valueForKey:@"isDone"]. Later I switched to a subclassed managed object namedJKItem, which is why the second set of methods is able to useitem.isDonewithout generating a compiler warning.