0

无法理解如何保存和加载选中/未选中的表格视图单元格。目前,我的代码仅适用于选中的单元格,但是当我取消选中标记时 - 所有对象都已从数组中删除,但我只想删除未选中的对象。

- (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];
}

我的列表 我的桌面视图

4

1 回答 1

2

试试这个:

 //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];
}
于 2013-11-08T13:16:48.697 回答