2014-02-13 3 views
0

попытку удалить файл, но когда я использую «indexPath.row», чтобы получить имя файла, он посылает меня всегда 0 ...Удалить из таблицы и удалить файл

Это мой код:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     NSError *error = nil; 
     NSFileManager *manager = [NSFileManager defaultManager]; 
     NSString *stringToPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/examples/"]; 
     NSString *stringToFile = [NSString stringWithFormat:@"%@/%@.extension", stringToPath, [self.examples objectAtIndex:indexPath.row]]; 
     [manager removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:stringToFile] error:&error]; 
     if(!error){ 
      NSLog(@"no error"); 
     }else{ 
      NSLog(@"%@", error); 
     } 
     [self.manageObjectContext deleteObject:[self.examples objectAtIndex:indexPath.row]]; 
     [self.manageObjectContext save:nil];   
     NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"Examples"]; 
     self.examples = [self.manageObjectContext executeFetchRequest:fetchReq error:&error]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

Это ошибка, когда я использую [self.examples objectAtIndex:indexPath.row]];

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. 
(Cocoa error 4.)" UserInfo=0xb92af90 {NSUnderlyingError=0xb92edb0 "The operation couldn’t be completed. 
No such file or directory", NSFilePath=.../Application Support/iPhone Simulator/7.0.3/Applications/8A5B551A-79A1-40E6-B1A4-C948D9F961D0/Documents/example/<Examples: 0xa2b3ef0> (entity: 
Examples; id: 0xa2b0310 <x-coredata:/6871E31A-1253-4195-A84D-61C71B002B72/Songs/p21> ; 
data: { 
     name = "Example name to delete"; 
    }).extension, NSUserStringVariant=(
     Remove 
    )} 

ответ

0

Я получаю имя файла так:

NSArray *fileName = [self.examples valueForKey:@"name"]; 
NSString *fileName2 = [fileName objectAtIndex:indexPath.row]; 
1

призыв к [self.examples objectAtIndex:indexPath.row], кажется, отдавая экземпляр Songs объекта. Ваше использование этого в stringWithFormat: приводит к вызову метода description для этого объекта. Это, безусловно, не то, что вы хотите.

Сплит код вверх немного:

Songs *songs = [self.examples objectAtIndex:indexPath.row]; 
NSString *filename = ... // some call on songs to get the filename 

Только вы знаете, как получить имя файла из songs объекта.

FYI - путь, по которому вы строите путь, является неправильным. Попробуйте найти подходящий способ получить ссылку на папку Documents. И не используйте stringWithFormat: для создания пути. Используйте stringByAppendingPathComponent:.

+0

Я использую это также, но это также не может помочь мне, что я не могу получить имя текущей строки (это который я удаляю) ... NSString * stringPath = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] stringByAppendingPathComponent: @ "/ examples /"]; Я хочу удалить файл, имя которого находится в таблице и т. Д. Моя строка в таблице «Имя примера для удаления», когда я нажимаю палец, чтобы удалить строку, я хочу удалить файл, но я не могу получить имя строки. Имя файла - это имя строки, и я не могу удалить файл, потому что я не могу получить имя строки в переменной ... –

Смежные вопросы