2

I am using the iPhone SDK and Objective-C and I get this error message:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:774 2010-01-08 13:24:16.842 MyAPP[628:20b] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).

Here is my code to expand sections:

- (void)checkAction:(id)sender
{
    //The button is added as subview on a view and the view is section header custom view
    UIButton *Button = (UIButton*)sender;
    NSLog(@"Button Tag=%d",Button.tag);
    if([[self.MySectionIndexArray objectAtIndex:Button.tag] isEqualToString:@"UP"])
    {
        [self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:@"DOWN"];
        [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Up.png"] forState:UIControlStateNormal];
        [TableDashBoard reloadSections:[NSIndexSet indexSetWithIndex:Button.tag] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        [self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:@"UP"];
        [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Down.png"] forState:UIControlStateNormal];
    }
    NSLog(@"self.MySectionIndexArray ka title = %@ at index Number=%d",self.MySectionIndexArray,Button.tag);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256

2 Answers2

6

The problem is because you're using reloadSections while changing the table's dataSource contents, so that it reports a different number of rows via tableView:numberOfRowsInSection:.

Check what you return from your UITableViewDataSource tableView:numberOfRowsInSection:. My bet is that at the beginning you return 0 for section 1, but when you call reloadSections, you return 1 for section 1.

If this is actually the case, you can use UITableView methods beginUpdates, insertRowsAtIndexPaths:withRowAnimation: and endUpdates.

Adam Woś
  • 2,493
  • 20
  • 21
  • I am not chnaging the dataSource.Actually if i use reloaddata the error doesn't comes. I am using expanding sections code from a stackoverflow post.but i am also gettng another strange problem here.i have created two Custom Cells From Interface Builder.I want to show seperator below both cells so i add a gray label to them.Now on my table view On first Section i allocoate one IB Cell for first row and for 3 more rows i alloc another IB cell.And for the rest of section alloc default cells.But when i scrolls down the gray label converted to white Do you anything related to this? – Rahul Vyas Jan 08 '10 at 12:28
  • What I meant was that you're changing the `dataSource`'s *contents* - i.e. what you return from `tableView:numberOfRowsInSection:` - this is directly said by the error message you're having. – Adam Woś Jan 08 '10 at 12:31
  • 1
    BTW, the fact that `reloadData` works indicates we're on track with trying to see what `tableView:numberOfRowsInSection:` returns. This is because calling `reloadSections` "causes the table view to ask its data source for new cells for the specified sections. The table view animates the insertion of new cells in as it animates the old cells out." And reloadData just reloads the data without any animations, and "should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates" – Adam Woś Jan 08 '10 at 12:46
  • @AdamWoś your detailed explanation here was what made me realize what I was doing wrong, thank you for that! I didn't realize a completely different section would cause problems when the table view was expecting updates to happen before that animation took place. – kyleturner Jan 29 '13 at 06:10
2

I had a similar problem. I was sure of being updating the dataSource.

But finally I realized that I was actually modifying 2 sections but updating the dataSource for only one.

Fede Mika
  • 2,211
  • 2
  • 19
  • 18
  • In a special case of that, when linking section reloading to KVO it is important that when reloading one section, only the number of rows for that single section changes. If something happens that affects the row count for multiple sections, then all those that changed should be passed in to `[reloadSections:withRowAnimation]`. – user3099609 Jul 28 '15 at 12:14