【发布时间】:2013-01-14 21:54:56
【问题描述】:
首先=我很抱歉,因为我在here之前已经尝试过问过一次
我真的很挣扎:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
通过使用上面的代码,我知道可以从 UITableView 中显示的数组中删除一个条目。但是,我想从我的文档目录中删除用户下载且不再需要的文件。
现在,我同时在更多搜索此代码后:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *file = [[NSString alloc] init];
for(int i=0; i<[paths count]; i++)
{
file = [documentsDirectoryPath stringByAppendingFormat:@"/%@",_fileArray];
NSLog(@"%@", file);
}
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:file error:NULL];
允许我在我的控制台中列出数组中的所有文件以及此代码:
- (void)removeFile:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(@"image removed");
}
连同:[self removeFile: _filename]; 允许我删除特定文件。
所以我正在努力。 但是当用户能够滑动和删除文件时,我真的被卡住了。当然我不知道目录中的文件是什么。
其次 - 删除所有文件后如何处理能够加载 tableView 的问题? 如果我使用此代码执行此操作:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0)
{
NSLog(@"Path: %@", [paths objectAtIndex:0]);
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
// Remove Documents directory and all the files
BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
}
我收到终止错误 - 我认为这是因为该目录也已被删除。
我知道这里有一些代码,但我真的希望有人指导我完成这个:-)
【问题讨论】:
-
您的问题看起来并不严格。你已经给出了这么多的解释。表示您要在删除表格单元格时从文档目录中删除单个文件或要从文档目录中删除所有文件。
-
我想删除一个文件,但它可以是目录中的任何文件。我事先不知道文件的名称。所以理想情况下,我想按下一个编辑按钮,选择一个或多个文件,然后选择删除。然后它们将从文档目录中删除
-
表示您在 UITableView 的文档目录中列出所有文件,然后删除您想要的文件。
标签: ios objective-c nsfilemanager nsdocument