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

  • Home
  • SEARCH
  • 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 7084425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:59+00:00 2026-05-28T07:17:59+00:00

I am getting this error when trying to delete a row out of a

  • 0

I am getting this error when trying to delete a row out of a UITableView. If I delete the last row in the tableview is does not error out and everything works fine but any other row throws an exception. Can someone tell me what I am doing wrong here? Any help would be much appreciated!

Error:

‘Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’

Code

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

    //1. clear existing URL's
    NSURL *urlToDelete = nil;

    //2. If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSInteger row = [indexPath row];
        urlToDelete = [documentURLs objectAtIndex:row];
        //[documentURLs removeObjectAtIndex:row];      
    }
    //3. update the tableview on the fly without reloading the data
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];

    //4. get the location of the files
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathToDocumentsDirectory = [paths objectAtIndex:0];

    //5.setup file manager
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    BOOL fileExists = [fileManager fileExistsAtPath:pathToDocumentsDirectory];
    NSLog(@"Path to file: %@", pathToDocumentsDirectory);        
    NSLog(@"File exists: %d", fileExists);
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:pathToDocumentsDirectory]);

    //6. remove if matches
    if (urlToDelete) {
        BOOL success = [fileManager removeItemAtURL:urlToDelete error:&error];
        if (!success) NSLog(@"Error: %@", [error localizedDescription]);
    }
}
  • 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-05-28T07:18:00+00:00Added an answer on May 28, 2026 at 7:18 am

    You have to udpate the table in three stages.

    1) Deal with your model

    [documentURLs removeObjectAtIndex:row];
    

    2) Deal with your table’s animation

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    

    3) Deal with your file update (maybe it could be done before because you could have error on file deletion)

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
       NSURL *urlToDelete = nil;
    
       if (editingStyle == UITableViewCellEditingStyleDelete) {
           NSInteger row = [indexPath row];
           urlToDelete = [documentURLs objectAtIndex:row];
           [documentURLs removeObjectAtIndex:row]; // you need to update your model
    
           [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]    withRowAnimation:UITableViewRowAnimationFade];
    
           // do stuff to update the file here
        }
    }
    

    or

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSURL *urlToDelete = nil;
    
        if(editingStyle == UITableViewCellEditingStyleDelete) {
            NSInteger row = [indexPath row];
            urlToDelete = [documentURLs objectAtIndex:row];
    
            // do stuff to update the file here...
    
            if(success)
            {
                [documentURLs removeObjectAtIndex:row]; // you need to update your model
    
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]    withRowAnimation:UITableViewRowAnimationFade];
            }
       }
    }
    

    Note. You have to pay attention if you remove the item from your model and the model count is eqaul to 0. In that case you have to remove the entire section.

    Hope it helps.

    P.S. check the code because I’ve written without XCode

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

Sidebar

Related Questions

I'm trying to figure out why I am getting this error when my Magento
I keep getting this error when trying to delete a record from my table.
While trying to migrate, I keep getting this error: rake aborted! test-unit is not
I am new to php and I am getting this error trying to load
I´m getting this error while trying to commit to a svn repository: svn: MKACTIVITY
I am getting this error when trying to run internet information services on a
I'm getting this error after trying to appendChild to an element that was just
I'm getting this error when trying to access my webservice running inside tomcat. Caused
I am getting this error when trying to merge TFS2008. There are no pending
I keep getting this error while trying to modify some tables. Here's my code:

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.