【问题标题】:ios - delete single files that are downloaded into the documents directoryios - 删除下载到文档目录中的单个文件
【发布时间】: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


【解决方案1】:

如果我理解正确,您需要分步指南。 假设你有一个工作的 UITableView (mainTableView) 从 NSMutableArray (mainArray) 获取数据。

这将返回文档目录路径。

-(NSString*)filePath{
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSLog(@"%@",path);
return path;
}

我们需要在某个地方初始化数组,我是在 viewDidLoad 上完成的。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    mainDataArray=[[NSMutableArray alloc]init];
    [self loadContentsOfDirectory:[self filePath]];
}

//here i am  adding names of files from documents directory
-(void)loadContentsOfDirectory:(NSString*)path
{
    NSError *error=nil;
    NSArray *pathArray=[[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&error];

   if (error) {
       NSLog(@"ERROR: %@",error);
   }else{
       if (pathArray) {
           [mainDataArray removeAllObjects];
           [mainDataArray addObjectsFromArray:pathArray];
       }
   }
}

#pragma mark UITableView_Methods

//delete file then if file is deleted , remove filename from array and remove cell
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
       NSString *fileName=[mainDataArray objectAtIndex:indexPath.row];

       NSError *error=nil;
       NSString *pathToDelete=[[self filePath]stringByAppendingPathComponent:fileName];
       BOOL succes=[[NSFileManager defaultManager]removeItemAtPath:pathToDelete error:&error];

       if (error) {
           NSLog(@"ERROR: %@",error);
       }

       // if file is succes deleted
       if (succes) {
           //remove this item from array 
           [mainDataArray removeObject:fileName];
           //and remove cell 
           [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       }else{
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Alert!" message:@"File can not be deleted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
           [alertView show];
       }
   }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return [mainDataArray count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reusableCell=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reusableCell];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell];
    }
    cell.textLabel.text=[mainDataArray objectAtIndex:indexPath.row];

   return cell;
 }

我认为你的错误是你没有删除数组中的单元格、对象。 希望这会有所帮助。

【讨论】:

  • 是的,我想就是这样,在你回答之前我就已经弄清楚了 - 但你的工作很棒:-) 100+
【解决方案2】:

如果您想从Documents 目录中删除选定的文件,那么。

首先,您必须像这样从应用程序的Documents 目录中获取所有文件

-(NSArray *)listFileAtPath:(NSString *)path
{
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    return directoryContent;
}

然后使用上述函数的返回数组将文件显示到UITableView

NSArray *filesArray = [self listFileAtPath:documentDirectoryPath];

最后,当您删除选定的文件时,您必须从Documents 目录和filesArray 中删除该文件,以管理UITableView,如下所示:

- (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];

        // Remove file from document directory 
        NSError *error = nil;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];

        // Remove file entry from filesArray
        [filesArray removeObjectAtIndex:indexPath.row];
    }
}

如果你想做这样的事情,希望会帮助你。

【讨论】:

  • 你在哪里声明了documentDirectoryPath??而且我无法将字符串路径读入其他任何地方。
  • 我没有声明 documentDirectoryPath。这只是为了表明您必须提供 Documents 目录路径来代替 documentDirecotoryPath。或者你可以像 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0];
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 2012-10-21
  • 2023-04-03
  • 2012-11-11
  • 2015-10-20
  • 1970-01-01
  • 2017-02-13
  • 2011-08-06
相关资源
最近更新 更多