我有一个表视图,其中包含从 plist 加载的类别名称列表。我想动态删除、添加和重新排序列表并将其保存到 plist。我使用了下面的代码,它在模拟器上运行良好,但在设备上崩溃。
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。如果我删除该行,它也在设备上工作但没有用。