0

我正在使用 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;
}
)

这与我得到的结果相同

4

2 回答 2

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];
于 2014-07-08T03:15:05.410 回答
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];
}

谢谢,我希望这段代码对某人有所帮助。

于 2014-07-27T19:35:36.623 回答