gpt4 book ai didi

ios - 使用复选标记从 TableView 加载和保存数据到 plist 中

转载 作者:行者123 更新时间:2023-11-28 22:22:48 26 4
gpt4 key购买 nike

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

- (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

最佳答案

试试这个:

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

关于ios - 使用复选标记从 TableView 加载和保存数据到 plist 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19859806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com