我正在使用 tableView 使用 plist 文件进行一些配置设置,以填充我正在处理此代码大约 2 周的数据,这似乎有效。
这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"] isEqual:@"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[week objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[week objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
}
[[week objectAtIndex:indexPath.row] writeToFile:@"week.plist" atomically:YES];
NSLog(@"%@, %@", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]);
NSLog(@"Array de week seleccionando %@", week);
}
在调试区域似乎一切正常(这就是我使用 nslogs 的原因),我的问题是无法保存 plist 文件中的更改,它似乎保存在某种缓存或类似的东西中。
更新
我也试过这段代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"week.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:week forKeys:week];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"] isEqual:@"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSString *uno = @"1";
[[week objectAtIndex:indexPath.row] setObject:uno forKey:@"checkmark"];
}
else if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"] isEqual:@"1"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
NSString *cero = @"0";
[plistData writeToFile:plistPath atomically:YES];
}
[[week objectAtIndex:indexPath.row] setObject:cero forKey:@"checkmarck"];
NSLog(@"%@, %@", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]);
NSLog(@"Array de checkmarks %@", week);
}
我在调试区也有同样的结果:
2014-07-07 22:11:21.384 experiment table view[2264:60b] Thursday, 0
2014-07-07 22:11:21.385 experiment table view[2264:60b] Array de week seleccionando (
{
Name = Sunday;
checkmarck = 0;
},
{
Name = Monday;
checkmarck = 1;
},
{
Name = Tuesday;
checkmarck = 0;
},
{
Name = Wednesday;
checkmarck = 0;
},
{
Name = Thursday;
checkmarck = 0;
},
{
Name = Friday;
checkmarck = 1;
},
{
Name = Saturday;
checkmarck = 0;
}
)
这与我得到的结果相同