【问题标题】:commitEditingStyle and delete file from document directorycommitEditingStyle 并从文档目录中删除文件
【发布时间】:2011-08-06 09:41:42
【问题描述】:

我正在尝试使用commitEditingStyle 方法从文档目录中删除文件,但我面临以下问题:

在表格中,我显示了保存在文档目录中的所有文件名。当用户按下“编辑”时,所有单元格都被激活以进行删除,现在如果用户按下红色按钮并删除所有文件加上文档目录删除,它应该一次删除一个文件,而不是所有目录。以下是代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);   
NSString *documentsDirectoryPath = [paths objectAtIndex:0];  

NSMutableString *File = [documentsDirectoryPath stringByAppendingPathComponent:fileNameString];  

NSFileManager *fileManager = [NSFileManager defaultManager];  
[fileManager removeItemAtPath:File error:NULL];  

[self.downList removeObjectAtIndex:indexPath.row];  


[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];  
    }     
}  

我希望有人知道问题出在哪里。

【问题讨论】:

  • 请更加努力地正确格式化您的代码。
  • 对不起垫,下次会做,感谢格式化代码
  • 什么是文件名字符串?请在删除元素之前和之后使用 NSLog(@"File :%@",File) 和 self.downList。
  • 这包含文件名 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath dequeueReusableCellWithIdentifier:CellIdentifier];单元格 = [tableView cellForRowAtIndexPath:indexPath]; fileNameString = [NSString stringWithString:cell.textLabel.text]; }
  • 也只列出一个数组,其中包含存储在文档目录中的所有文件名,我正在使用此数组在单元格中显示

标签: objective-c iphone file nsfilemanager delete-file


【解决方案1】:

好的,我想通了,我想当我按下“删除”按钮时 cell.text.text 将被“didSelectRowAtIndexPath”方法读取,但我错了,所以现在我在以下步骤中实现了代码
1. 当用户选择任何单元格时,会弹出操作表
2. 用户可以从操作表中选择“删除”选项
3. 选择“删除”选项后,将执行删除操作并删除特定文件

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2013-02-07
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多