编辑TableView并更新plist

时间:2012-09-28 09:15:11

标签: iphone ios uitableview plist objective-c-blocks

我有一个表视图,其中包含从plist加载的类别名称列表。 我想动态删除,添加和重新排序列表并将其保存到plist.i使用下面的代码,它在模拟器上运行良好,但它在设备上崩溃。

In ViewDid Load:
 self.categoryFile = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"plist"];
 self.categoryList = [[NSMutableArray alloc]initWithContentsOfFile:self.categoryFile];

- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
    [[self categoryList] removeObjectAtIndex:[indexPath row]];
    NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];
    [tableView reloadData];
    NSArray *newArr = [[NSArray alloc]initWithArray:self.categoryList];
    [newArr writeToFile:self.categoryFile atomically:YES];
    [newArr release];
}

}

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{

NSString *contentsToMove = [[[self categoryList] objectAtIndex:[fromIndexPath row]] retain];
[[self categoryList] removeObjectAtIndex:[fromIndexPath row]];
[[self categoryList] insertObject:contentsToMove atIndex:[toIndexPath row]];
NSArray *newArr = [[NSArray alloc]initWithArray:self.categoryList];
[newArr writeToFile:self.categoryFile atomically:YES];
[newArr release];
[contentsToMove release];
}

错误发生在writeToFile:atomically.if我删除了那条线,它也在设备上工作但没有用。

3 个答案:

答案 0 :(得分:0)

您无法写入设备上的捆绑资源。首次需要时将plist复制到应用程序的文档目录,然后从那里开始读取/更新。

代码改编自answer to this question:

- (NSString *)pathToPlist
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Categories.plist"];
    BOOL copyProblem;
    if ([fileManager fileExistsAtPath:plistPath] == NO) {
        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"];
        copyProblem = [fileManager copyItemAtPath:resourcePath toPath:plistPath error:&error];
    }

    if (copyProblem)
        return nil;

    return plistPath
}

将此分配给self.categoryFile,而不是调用NSBundle。代码未编译或测试,因此应考虑详细的伪代码,请准备纠正小错字。

答案 1 :(得分:0)

如果要稍后编辑并保存文件,则必须首先复制NSDocumentsDirectory中的文件(如果它不存在)。见Link for reference

加载你的plist

BOOL success;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:@"your_file" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];

success = [fileManager fileExistsAtPath:path];
if (success) {
    self.selectedObjectsDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];       

}
else
{    

    success = [fileManager copyItemAtPath:filePath  toPath:path error:&error];
    self.selectedObjectsDict=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath];


}

用于保存plist

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];
[your_dict writeToFile:path atomically:YES];

答案 2 :(得分:0)

您要做的是从[NSBundle mainBundle]中删除一个文件,这个文件永远不会发生,因为这些文件是只读的。

如果要删除和创建文件,请在DocumentsDirectory

中尝试

请尝试使用此link作为参考