70

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?

craig1231
  • 3,515
  • 3
  • 26
  • 31
user3085646
  • 895
  • 2
  • 9
  • 14

4 Answers4

158

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];
Undo
  • 25,204
  • 37
  • 102
  • 124
  • 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. – user3085646 Feb 19 '14 at 04:02
  • 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 Feb 19 '14 at 04: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. – Alix Apr 24 '14 at 23:35
  • why is the array so interlocked to the dataSource? Does `deleteRowsAtIndexPaths` just automatically call reloadData? and then sees a mismatch? – Honey Jan 10 '17 at 18:51
14

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)
    }
}
6

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.

Haseeb Iqbal
  • 304
  • 2
  • 9
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
}
Pranav Kasetti
  • 4,773
  • 2
  • 16
  • 38
7RedBits.com
  • 148
  • 2
  • 3