ios - 从 NSDirectory 中删除多个文件

标签 ios objective-c uitableview

我已经为 iOS 创建了一个应用程序,它像文档查看器一样工作,我可以在其中查看文档,而且它的工作方式就像 charm 一样。我在表格 View 中列出了文件,并提供了删除文件和单个文件的滑动功能。我在编辑过程中也启用了多重选择。 TableView 行被选中,但我不确定如何从 NSDirectory 中删除选定的文件。谁能帮忙?

我在 didSelectRowForIndexPath 中有这个:

  if (self.tableView.isEditing)
{
    [_selectedRows arrayByAddingObject:[self.tableView indexPathsForSelectedRows]]; 
}

按下删除按钮时回答这个问题,

- (void)deleteButton:(id)sender
{
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    for (id object in _selectedRows) {
        UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:object];

        NSString *cellText = selectedCell.textLabel.text;
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];

        NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];

        NSString *theactualFilePath = [allFilesPath stringByAppendingPathComponent:cellText];    

        NSError *error;

        _success = [[NSFileManager defaultManager] removeItemAtPath:theactualFilePath error:&error];
    }

 }

这就是我填充表格 View 单元格的方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"paths :%@",paths);
textlabel = [[UILabel alloc] initWithFrame:CGRectMake(15,0, 250, 70)];



NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];

NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];



if([[userDefaults objectForKey:@"searchON"] isEqualToString:@"yes"]) {

    NSString *theFileName = [[_filePathsArrayCopy objectAtIndex:indexPath.row] lastPathComponent] ;

    textlabel.text = theFileName;
}
    else {

  NSString *theFileName = [[_filePathsArray objectAtIndex:indexPath.row] lastPathComponent] ;



    textlabel.text = theFileName;
}
[cell.contentView addSubview:textlabel];


return cell;

最佳答案

因为帖子上的评论太多了,所以我只发布了一个可能的解决方案。

- (void)deleteButton:(id)sender
{
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    NSString *docPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *inboxPath = [docPath stringByAppendingPathComponent: @"/Inbox/"];

    for (NSIndexPath *indexPath in indexPaths) {
        NSString *fileName = [[_filePathsArrayCopy objectAtIndex:indexPath.row] lastPathComponent] ;
        NSString *fullFilePath= [inboxPath stringByAppendingPathComponent:fileName ];

        NSError *error;
        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:fullFilePatherror:&error];
        if (error != nil) {
            print(error)
        }
    }

}

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

相关文章:

ios - 平移手势干扰滚动

ios - 将 UITableView 添加为 subview

ios - 如何在上一个选定行的 didDeselectRow 处知道下一个选定行的 IndexPath?

ios - 放大用户位置 - Swift

ios - NSFileManager.defaultManager().createFileAtPath() 返回 false

ios - 揭开 iPad 上被键盘隐藏的内容

ios - 无法清除 UIWebView 缓存

objective-c - 修改 NSScroller 的长度

objective-c - 将文本从 uitextview 绘制为 pdf

ios - TableViews 和单独的列