0

I have the following code written for my delete button to delete selected rows from the UITableView.

-(IBAction)deleteItems:(id)sender {

 NSArray *selectedCells = [self.autoCompleteView indexPathsForSelectedRows];
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in selectedCells) {
    [indicesToDelete addIndex:indexPath.row];
}
//arrayFromPlist is NSMutableArray
[autoCompleteView beginUpdates];
[autoCompleteView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
[autoCompleteView endUpdates];
[selectedObjects removeObjectsAtIndexes:indicesToDelete];
[autoCompleteView reloadData];

[alertMsg deleteConfirmation:@"Do you want to delete these items?"];

}

Please check the image for my UITableView and the delete button. I have kept the Editing property of my UITableview to "Multiple Selection during Editing" in the storyboard.

enter image description here

I get the following error when i press the Delete button shown in the screen.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '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, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Not sure what is my mistake in the delete button code.

Community
  • 1
  • 1
TheGaME
  • 443
  • 2
  • 8
  • 21

1 Answers1

1

One definite problem is that you are removing the cells before you have removed the corresponding data from the model. That's wrong. Always change the model first; then change the cells in such a way as to match the model.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • @matt...I put the [selectedObjects removeObjectsAtIndexes:indicesToDelete]; below BeginUpdates but still get the same error as mentioned in the question. – TheGaME Nov 04 '15 at 03:51
  • I don't know what `selectedObjects` is or how your model is maintained. But you must delete the rows from the model before you start your `beginUpdates`. – matt Nov 04 '15 at 03:54
  • I have 2 mutable arrays: selectedObjects will contain items to be deleted and autoCompleteData contains the initial data to be displayed in the tableview. – TheGaME Nov 04 '15 at 03:57
  • @matt..Got it. small mistake i made. I changed the selectedObjects to autocompleteData. it worked. – TheGaME Nov 04 '15 at 03:58
  • Then it is `autoCompleteData` that you need to be modifying. If it is the source of the data in your `cellForRowAtIndexPath` you must modify it now, and you must modify it first. Here is sample code that works: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p445deleteTableRows/ch21p718sections/RootViewController.swift Look at how I modify the model completely before deleting any rows of the table view. – matt Nov 04 '15 at 03:58
  • @matt..If my tableview contains only 1 sections always then in the numberofSections method can i return 1. – TheGaME Nov 04 '15 at 04:01