3

Firstly = I apologize because I have already tried to ask this once before here

I am really struggling with this:

// 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];
    }

}

By using the code above I know it is possible to delete an entry from an array that is displayed in a UITableView. However I am wanting to delete files form my Documents Directory that are downloaded by the user and no longer required.

Now I in the mean time and after more searching this code:

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

Allows me to list all the files that are in the array in my console and this code:

- (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");

}

together with: [self removeFile: _filename]; allows me to delete a specific file.

so I am making head way. But I am really stuck when it comes to a user being able to swipe and delete a file. Of Course I do not know what file is going to be in the directory.

Secondly - How do I handle being able to load the tableView once all the files are deleted? If I do that using this code:

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


}

I get the an termination error - I think that is because the directory has also been removed.

I know there is a bit of code here, but I really hope someone and guide me through this:-)

Community
  • 1
  • 1
jwknz
  • 6,598
  • 16
  • 72
  • 115
  • Your question doesn't look strait. You have given so much explanation. Means you want to delete single file from document directory as you delete table cell or want to delete all files from document directory. – Yuvrajsinh Jan 17 '13 at 05:36
  • I want to delete a single file, but it could be any file in the directory. I do not know the name of the file before hand. So ideally I want to press an edit button, select one or multiple files and then tab Delete. They will then be removed from the Documents Directory – jwknz Jan 17 '13 at 08:54
  • Means you are listing all files in document directory in UITableView and then delete file which you want. – Yuvrajsinh Jan 17 '13 at 10:35

2 Answers2

1

If you want to delete selected files from your Documents directory then.

First you have to get all files from Documents directory of your app like this

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

Then use return array from above function to display files to UITableView

NSArray *filesArray = [self listFileAtPath:documentDirectoryPath];

And finally when you delete selected files then you have to remove that file from Documents directory and also from filesArray for managing UITableView something like this:

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

Hope will help you if you want to do something like this.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • Where did you declare documentDirectoryPath?? and also I cannot read the string path into anywhere else. – jwknz Jan 17 '13 at 17:58
  • I have not declared documentDirectoryPath. It is just for indicating that you have to provide Documents directory path in place of documentDirecotoryPath. OR You can do like NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; – Yuvrajsinh Jan 18 '13 at 07:11
1

If i correctly understood, you want step by step guide. Lets consider that you have a working UITableView (mainTableView) that take data from NSMutableArray (mainArray).

This will return document directory path.

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

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

Somewhere we need to init array ,i did it on 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;
 }

I think your error is that you do not remove something maybe cell, object in array. Hope this will help.

user982705
  • 1,973
  • 1
  • 15
  • 11
  • Yup I think that was it, I had it figured out before your answer - but your one worked great:-) 100+ – jwknz Jan 19 '13 at 23:59