ios - 从表中删除并删除文件

标签 ios file uitableview delete-file

我尝试删除该文件,但当我使用“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
    )}

最佳答案

[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:

关于ios - 从表中删除并删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744221/

相关文章:

objective-c - 如何在 iOS 中加载 XIB

iphone - UIImageView 不一致地变黑

c# - 使用 TCP 客户端接收多个文件

javascript - 正在下载的文件没有正确的名称和扩展名

swift - UITableViewAutomaticDimension 及其高度

ios - 长按手势识别器干扰 UITableView 中的滚动

iphone - iPhone开发中的图形设计问题

ios - 压缩 UIImage

ios - UIButton 不会调用@IBAction

c - 用c语言打开和写入文件