0

Can't understand how to save and load checked / unchecked tableview cells. For now, my code works for only checked cells, but when I take off the checkmark – all objects have removed from array, but I want to remove only unchecked object.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    bool isSelected = (cell.accessoryType == UITableViewCellAccessoryCheckmark);
    cell.accessoryType = isSelected ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;

    //loading my whole plist to overwrite it then
    NSString *path = [DOCUMENTS stringByAppendingPathComponent:@"userData.plist"];
    NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:path];

    //loading fragment of plist with personal qualities which I want to check / uncheck in my tableview
    NSMutableArray *oldData = data[@"myObjects"];

    //new array to overwrite the old
    NSMutableArray *newData=[[NSMutableArray alloc] init];

    if (isSelected) {
        [newData removeObject:cell.textLabel.text]; //don't know  where I should paste the code for removing object, this line no matter doesn't works
    } else {
        [newData addObjectsFromArray:oldData];
        [newData addObject:cell.textLabel.text];
    }

    [data setObject:newData forKey:@"myObjects"];
    NSData *dataToPlist = [NSPropertyListSerialization dataWithPropertyList:data
                                                                     format:NSPropertyListXMLFormat_v1_0
                                                                    options:0
                                                                      error:nil];
    [dataToPlist writeToFile:path atomically:YES];
}

My plist My tableview

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

2

Try this:

 //new array to overwrite the old
NSMutableArray *newData=[[NSMutableArray alloc] initWithArray:oldData];

if (isSelected) {
    [newData removeObject:cell.textLabel.text]; //don't know  where I should paste the code for removing object, this line no matter doesn't works
} else {
    [newData addObject:cell.textLabel.text];
}
Alex
  • 79
  • 4
  • Thanks, it works. Can you help me with adviсe? How can I load / save the "state" of the cell, is it checked or unchecked? – Alexander Yakovlev Nov 08 '13 at 15:25
  • Since table view cell can be reused in long lists you should better use some container(array,dictionary,...) to store actual list data or NSFetchedResultsController to display data from Core Data storage. See [link1](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html) and [link2](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP40001075) for more details. – Alex Nov 08 '13 at 17:37