2012-01-21 5 views
0

AppleのサンプルコードDirectoryWatcherとDITableViewControllerによって読み込まれたテーブルビューからファイルを削除しようとしています。ここで私のコードは、私は個々のファイルではなく、配列全体を削除するようです。アレイ全体ではなくパスを削除する

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    // If row is deleted, remove it from the list. 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSInteger row = [indexPath row]; 
     [documentURLs removeObjectAtIndex:row];  
    } 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
        withRowAnimation:UITableViewRowAnimationFade]; 


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *pathToDocumentsDirectory = [paths objectAtIndex:0]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    BOOL fileExists = [fileManager fileExistsAtPath:pathToDocumentsDirectory]; 
    NSLog(@"Path to file: %@", pathToDocumentsDirectory);   
    NSLog(@"File exists: %d", fileExists); 
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:pathToDocumentsDirectory]); 
    if (fileExists) 
    { 
     BOOL success = [fileManager removeItemAtPath:pathToDocumentsDirectory 
               error:&error]; 
     if (!success) 
      NSLog(@"Error: %@", [error localizedDescription]); 
    } 
} 

誰かが私にはちょうど配列のうちの1つのファイルを削除する方法を見つけ出す手助けすることはできますか?

ありがとうございます!

答えて

0

あなたのコードは、アプリケーションのDocumentsディレクトリを削除しようとします。

documentURLsアレイからURLを取り出して、-[NSFileManager removeItemAtPath:]に渡す必要があります。たとえば、次のように

NSURL *urlToDelete = nil; 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    NSInteger row = [indexPath row]; 
    urlToDelete = [documentURLs objectAtIndex:row]; 
    [documentURLs removeObjectAtIndex:row];  
} 

... 
if (urlToDelete) { 
    BOOL success = [fileManager removeItemAtPath:urlToDelete error:&error); 
    ... 
+0

速い応答ロブのおかげで、私はタイプのNSStringのパラメータにNSURLを送信し、互換性のないポインタ型であると、ファイルが – Dan

+0

'NSFileManager'も' removeItemAtURLという名前のメソッドがあるままの警告を取得:エラー: '。 –

+0

それはトリックロブ、素晴らしい助け、ありがとう! – Dan

関連する問題