2012-01-21 4 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]); 
    } 
} 

Может кто-нибудь помочь мне разобраться, как просто удалить один файл из массива?

Спасибо!

ответ

0

Ваш код пытается удалить каталог документов приложения.

Вам нужно вытащить URL-адрес из массива documentURLs и передать его -[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

Спасибо за быстрый ответ Робом, но я получаю предупреждение о том, что несовместимый тип указателя отправки NSURL к параметру типа NSString и файл остается – Dan

+0

'NSFileManager' также имеет метод с именем' removeItemAtURL: ошибка : '. –

+0

Это был трюк Роб, потрясающая помощь, спасибо !!! – Dan