84

I've read all of the related posts regarding this and I am still having an error:

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), 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).'

Here are the details:

in .h I have an NSMutableArray:

@property (strong,nonatomic) NSMutableArray *currentCart;

In .m my numberOfRowsInSection looks like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.


    return ([currentCart count]);

}

To enable delete and remove the object from the array:

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [currentCart removeObjectAtIndex:indexPath.row];


    }
}

I thought that by having my number of sections relying on the count of the array I'm editing it would ensure the proper number of rows? Can't this be done without having to reload the table when you delete a row anyway?

5 Answers 5

167

You need to remove the object from your data array before you call deleteRowsAtIndexPaths:withRowAnimation:. So, your code should look like this:

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [currentCart removeObjectAtIndex:indexPath.row];

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

You can also simplify your code a little by using the array creation shortcut @[]:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
4
  • Unbelievable that I overlooked that flawed logic. Simple solution and I was going crazy trying to figure out if my count was being passed incorrectly. Thank you very much. Commented Feb 19, 2014 at 4:02
  • 1
    No problem! I've done the exact same thing countless times before, and that's why I had a pretty good idea of what the problem was.
    – Undo
    Commented Feb 19, 2014 at 4:03
  • 1
    what if I am returning row number and literally. like as switch (section) { case 0: return 3; break; case 1: return 5; break; case 2: return 2; break; } return 0; there is no model array I am setting up. I am just learning table view. I found the problem, and I try to create modelarray and delete removeObjectAtIndex but still having same error.
    – Alok C
    Commented Apr 24, 2014 at 23:35
  • why is the array so interlocked to the dataSource? Does deleteRowsAtIndexPaths just automatically call reloadData? and then sees a mismatch?
    – mfaani
    Commented Jan 10, 2017 at 18:51
16

Swift Version --> Remove the object from your data array before you call

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        print("Deleted")

        currentCart.remove(at: indexPath.row) //Remove element from your array 
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}
11

In my case issue was that numberOfRowsInSection was returning similar number of rows after calling tableView.deleteRows(...).

Since this was the required behaviour in my case, I ended up calling tableView.reloadData() instead of tableView.deleteRows(...) in cases where numberOfRowsInSection will remain same after deleting a row.

0

Here is some code from above added with actual action code (point 1 and 2);

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in

        // 1. remove object from your array
        scannedItems.remove(at: indexPath.row)
        // 2. reload the table, otherwise you get an index out of bounds crash
        self.tableView.reloadData()

        completionHandler(true)
    }
    deleteAction.backgroundColor = .systemOrange
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
    configuration.performsFirstActionWithFullSwipe = true
    return configuration
}
0

I was using noDataCell when dataModel is empty...

and when I get the first row in my dataModel and update tableview row with the following code ...

 self.tableView.performBatchUpdates({
    tableView.insertRows(at: indexPaths, with: animation)
}, completion: nil)

through exception, the number of rows is still the same because it's expecting 2 numberOfRows and still gets one...

Two possible solutions for this is either reload tablview after dataModel got updated or make a separate View for noData instead of using noDataCell

Not the answer you're looking for? Browse other questions tagged or ask your own question.