2012-09-28 98 views
0

我有一個表視圖與從plist中加載的類別名稱列表。 我想動態刪除,添加和重新排列列表並將其保存到plist.i使用下面的代碼,它在模擬器上工作很好,但它在設備上崩潰。編輯一個TableView並更新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.if我也沒什麼用,但刪除了這一行其工作的設備。

回答

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。代碼沒有編譯或測試,因此應該考慮詳細的代碼,請準備好糾正小的錯別字。

+0

ü可以給我一些代碼,我不知道你在說什麼,而且我是新來的目標-C – Chandu

+0

每個應用程序都讀的想法 - 僅捆綁,然後一讀 - 編寫沙箱,其中包括文檔目錄。如果要在應用程序中寫入文件,可以將文件從軟件包複製到文檔目錄。代碼應該很容易挖掘 –

+0

@Chandu更新w /一些代碼。 –

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]; 
0

你所要做的是刪除[一個NSBundle mainBundle]文件可以永遠不會發生,因爲這些文件是隻讀的。

如果要刪除並創建文件,請在DocumentsDirectory中嘗試它

試試這個link參考

+0

可以給我提供一些代碼請 – Chandu

+0

查看我的更新回答 – AppleDelegate