0

I have problem with deleting rows from my tableView, because I have multiple sections that are dynamically generated when view controller appears, so when I return the count in numberOfRowsInSection it ends up looking like this:

NSInteger count = [[_sectionsArray objectAtIndex:section] count];
return count;

Now when deleting I generate this same type of array like this:

NSMutableArray *contentsOfSection = [[_sectionsArray objectAtIndex:[indexPath section]] mutableCopy];
[contentsOfSection removeObjectAtIndex:[indexPath row]];

As you can see I'm deleting the object from an array that is not linked to the tableView, so it just returns an NSInternalInconsistencyException

Can anybody help me with this?

UPDATE:

    [contentsOfSection removeObjectAtIndex:[pathToCell row]];

    if ([contentsOfSection count] != 0) {
        // THIS DOESN'T
        [self.tableView deleteRowsAtIndexPaths:@[pathToCell] withRowAnimation:UITableViewRowAnimationFade];
    }
    else {
        // THIS WORKS!
        [_sectionsArray removeObjectAtIndex:[pathToCell section]];
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];
    }
11
  • provide the complete exception Commented Feb 2, 2013 at 12:06
  • *** 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 (3) must be equal to the number of rows contained in that section before the update (3), 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).' Commented Feb 2, 2013 at 12:07
  • but the problem is not exactly the exception, the problem is deleting an object from a dynamically generated array and updating that array as the count in numberOfRowsInSection. Commented Feb 2, 2013 at 12:08
  • 1
    Can't you use a dictionary instead of storing arrays within arrays? Commented Feb 2, 2013 at 12:10
  • Remove the object before calling deleteRowsAtIndexPaths: Commented Feb 2, 2013 at 12:11

2 Answers 2

1

muatableCopy will create another instance of the array. So you are removing item from the newly created array not from the old one. Always store 'contentsOfSection ' as a mutable array in _sectionsArray. Then remove like this.

NSMutableArray *contentsOfSection = [_sectionsArray objectAtIndex:[indexPath section]];
[contentsOfSection removeObjectAtIndex:[pathToCell row]];

if ([contentsOfSection count] != 0) {

    [self.tableView deleteRowsAtIndexPaths:@[pathToCell] withRowAnimation:UITableViewRowAnimationFade];
}
else {
    // THIS WORKS!
    [_sectionsArray removeObjectAtIndex:[pathToCell section]];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];
}
Sign up to request clarification or add additional context in comments.

Comments

0

In following code :

[_sectionsArray removeObjectAtIndex:[pathToCell section]];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];

You are deleting object from _sectionArray. So this array automatically updates. But in other cases you are creating another copy and then deleting object from that array. So your _sectionArray is not going to update. So this is necessary that after deleting object from copy array, update section array with that new array also.

NSMutableArray *contentsOfSection = [[_sectionsArray objectAtIndex:[indexPath section]] mutableCopy];
[contentsOfSection removeObjectAtIndex:[indexPath row]];
[_sectionsArray replaceObjectAtIndex:[indexPath section] withObject:contentsOfSection];

Try this one and I hope that this will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.