2014-02-13 267 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對象的文件名。

僅供參考 - 您建立路徑的方式都是錯誤的。搜索正確的方法來獲取對Documents文件夾的引用。並且不要使用stringWithFormat:來建立路徑。使用stringByAppendingPathComponent:

+0

我用這個也但這也不能幫助我,我不能讓當前行的名稱(這個我刪除)... 的NSString * stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0] stringByAppendingPathComponent:@「/ examples /」]; 我想刪除表中的名字等文件我表中的行是「刪除的示例名稱」當我滑動我的手指刪除行我也想刪除文件,但我不能得到名稱該行。文件名是行的名稱,我無法刪除該文件,因爲我無法在變量中獲取行的名稱... –