0

I am attempting to use insertRowsAtIndexPaths so I can insert a table row. I am getting this error:

*** 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 (0) 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 (1 inserted, 0 deleted).'

What I am trying to do is to dynamically modify the number of rows in section in my attempt at rectifying the problem:

NSArray *indexPathIndex = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0   inSection:0], nil];    
[myTableView beginUpdates];
//***** HERE I AM TRYING TO DYNAMICALLY INCREASE NUMBER OF ROWS
[myTableView numberOfRowsInSection:1];
////////////////////////////////////////////////////////////
[myTableView insertRowsAtIndexPaths:indexPathIndex   withRowAnimation:UITableViewRowAnimationTop];
[myTableView endUpdates];

Initially, my number of rows in section is 0. I try to increase this number in my efforts to address the aforementioned error however the code [myTableView numberOfRowsInSection:1] seems to have no effect.

Could somebody please suggest how I can fix this problem?

Thank you, Victor.

csano
  • 13,266
  • 2
  • 28
  • 45

3 Answers3

0

You should not be setting the number of rows yourself, the UITableView manages that on its own. Remove the line.

myTableView numberOfRowsInSection:1]; 
Perception
  • 79,279
  • 19
  • 185
  • 195
  • Hi. I have removed this line as you advised but I don't know any other way to fix the problem "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 (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted)." - There must be some way to change the number of rows as this error message is asking me to do, right? – victor2010 Jun 24 '11 at 16:19
0

Take a look at the UITableViewDataSource protocol, particularly the tableView:numberOfRowsInSection method. This will allow you to dynamically set the number of rows in the section.

csano
  • 13,266
  • 2
  • 28
  • 45
0

A tableView has a datasource that is generally an array, that you can use to fill the cells of the tableView.

- (UITableViewCell*)tableView cellForRowAtIndexPath..... {
    //Dequeueing or alloc-init-ing a cell
    SomeClass *someObject = [someArray objectAtIndex:indexPath.row]; // Something
    // like this, where someArray becomes the source array.
    ...
}

Now in some other part of your code, if you want to dynamically change the contents of the tableView, change the someArray and then change the tableView.

[someArray insertObjectAtIndex:someIndex];
[myTableView insertRowsAtIndexPaths....];  //The indexPath is generally
// consistent with the someIndex.
Sailesh
  • 25,517
  • 4
  • 34
  • 47
  • Hi everyone. Thank you so much for your replies. I modified my implementation by making a new class that subclasses UITableViewController instead of UIViewController and everything worked fine after that. Not sure why it didn't work in UIViewController class though. Thanks again for your assistance. – victor2010 Jun 27 '11 at 18:29