Reputation: 6384
I keep running into an assertion failure but I am not sure how to resolve.
Initially I did a proof of concept app to understand how this functions and had this code in it:
if (self.arrNonATIResults.count > 0) {
NSMutableArray* arrIndexPaths = [NSMutableArray array];
for(int i=0; i<[self.arrNonATIResults count]; i++) {
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[arrIndexPaths addObject:anIndexPath];
}
[self.accountTable beginUpdates];
[self.accountTable deleteRowsAtIndexPaths:arrIndexPaths withRowAnimation:UITableViewRowAnimationTop];
self.arrNonATIResults = [NSMutableArray array];
[self.accountTable endUpdates];
}
It worked fine. Moving that code into my app I ran into this assertion failure:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) 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, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
Here's where I am getting confused, self.arrNonATIResults (the table datasource) contains two objects (it's hardcoded) - so the table has only two rows. Where is the 3rd row in the error message coming from? I did read that if you remove all rows then you also have to remove the section as well. Is that the third item in the error message?
So I rewrote my initial code but this still shouldn't work because the arrIndexPath array is only going to include 2 NSIndexPaths. Can anyone point me in the right direction as to what I am doing wrong?
- (void) clearNonATITable {
if (self.arrNonATIResults.count > 0) {
NSMutableArray* arrIndexPaths = [NSMutableArray array];
for(int i=0; i<[self.arrNonATIResults count]; i++) {
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[arrIndexPaths addObject:anIndexPath];
}
[self.accountTable beginUpdates];
for (int i=0; i<arrIndexPaths.count; i++) {
NSIndexPath* thisIndexPath = [arrIndexPaths objectAtIndex:i];
if (self.arrNonATIResults.count > 0) {
[self.accountTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:thisIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[self.arrNonATIResults removeObjectAtIndex:0];
} else {
NSIndexSet* setIndexSections = [[NSIndexSet alloc] initWithIndex:thisIndexPath];
[self.accountTable deleteSections:setIndexSections withRowAnimation:UITableViewRowAnimationFade];
}
}
[self.accountTable endUpdates];
}
}
Upvotes: 0
Views: 118
Reputation: 6384
The issue was the app was using 3 datasources for this one tableview - the datasource would change based on a segment control selection. I just needed to determine which source it was and use it's count for the number of NSIndexPaths to remove.
Upvotes: 0