我使用tableView使用tableView进行一些混淆设置来填充我正在使用此代码的数据大约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);
}
在debbug区域似乎一切正常(这就是我使用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;
}
)
这与我得到的结果相同
答案 0 :(得分:0)
如果Plist在捆绑中,则无法写入。 '在捆绑中'表示您通过手动在XCODE中添加新文件来创建Plist。
您只能写入以编程方式创建的Plist。这样的Plist不在捆绑包中,而是在应用程序的文档目录中。 Plist的路径是
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = @"week.plist";
NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName];
答案 1 :(得分:0)
感谢Sen我解决了这个问题,他的回答让我纠正了我的代码。
首先,我必须以编程方式创建pList:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"week.plist"];
NSMutableArray *week = [[NSMutableArray alloc]init];
//Here I add all the objects inside the Mutable Array
[week writeToFile:path atomically:YES];
然后通过在选择器上选择它来写入Plist:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"week.plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[array objectAtIndex:indexPath.row] objectForKey:@"checkmark"] isEqual:@"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[array objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[array objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
}
[array writeToFile:path atomically:YES];
[tableView reloadData];
[self.navigationController pushViewController:self animated:YES];
}
谢谢,我希望这段代码可以帮助别人。