无法理解如何保存和加载已检查/未检查的tableview单元格。目前,我的代码仅适用于已检查的单元格,但是当我取消选中标记时 - 所有对象都已从数组中删除,但我只想删除未选中的对象。
- (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];
}
答案 0 :(得分: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];
}