Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:22:54+00:00 2026-06-17T13:22:54+00:00

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

  • 0

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:-)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:22:55+00:00Added an answer on June 17, 2026 at 1:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly apologies if this is really simple but I have spent hours trying and
Firstly I have tried to re create this within a JSfiddle but unable to,
Firstly I hate IE but we all have to support it! I have a
firstly, I very much apologize for my poorly written title of this question. So
Firstly, I'm new to Android (so I apologize if this question is ignorant) but
Firstly I would say that I have read this post however I still have
Firstly you'll find my site here. I have div: <div class=home style=background-image: url(http://payload51.cargocollective.com/1/7/237315/3336908/HomeImage_o.jpg); background-attachment:
Firstly, I have done my research and already know that JavaScript = client JSPs
Firstly I have a class signature that looks like this: internal class JobStore :
Firstly, bear with me here. I have a custom model binder which is successfully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.