Could somebody explain what I’m doing wrong in attempting to insert a new UITableViewCell? I’m trying to insert a custom UITableViewCell, but it throws the following error: ‘Invalid update: invalid number of rows in section 0. 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 (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0)
return 1;
else if (section == 1)
return numberOfRows;
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EditableCell";
EditableCell *editableCell = (EditableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (editableCell == nil) {
editableCell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_myTextField = [editableCell textFieldCell];
if (indexPath.section == 0){
[_myTextField setPlaceholder:@"Menu Name"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else if (indexPath.section == 1){
[_myTextField setPlaceholder:@"Category"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else {
[_myTextField setPlaceholder:@"Category"];
[_myTextField setReturnKeyType:UIReturnKeyDone];
}
_myTextField.keyboardType = UIKeyboardTypeDefault;
_myTextField.delegate = self;
return editableCell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.returnKeyType == UIReturnKeyNext) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
NSArray *array = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
numberOfRows = numberOfRows + 1;
[self.tableView reloadData];
}
}
You need to make sure that the number of rows returned in the method below now returns the correct number of rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionI am sure what is happening is that you are returning the same value here after your insert, whereas the runtime is expecting it to be one more than it was before you made the call to insert a row.
In a bit more detail, what is happening in MVC terms (Model/View/Controller) is this:
insertRowsAtIndexPathsis requesting that the Controller update the View with an additional row.The Controller then does a sanity check to see if you have your sums right, so it calls:
tableView:numberOfRowsInSectionNow the controller knows what value this method returned last time (in your case, 1), and it also knows that you have requested to insert a row. There have been no delete calls, so it expects the next time it calls this method, the value should be (last time’s value + rows inserted – rows deleted) = 2
If the Controller deems that you have things in order, it will then call
cellForRowAtIndexPath(along with other methods) to get the actual cells for each of the rows in each section.So for your problem, you need to keep track of the rows in your model – maybe in an array or an ivar with the count of rows available. Update your model by updaing this value as you add/delete rows before you make the call to
insertRowsAtIndexPath, and then return this value whentableView:numberOfRowsInSectionis called.Additional tip:
If you wish to have your cell inserts/deletes to be animated, change your code to the below. Notice there is no call to
reloadDataanymore, instead the insert/reload is wrapped in begin/end update calls – The animated updates will occur afterendUpdatesdue to thereloadSectionscall.Check out the WWDC 2010 video from Apple, “Mastering Table Views” – explains everything really nicely, regarding using
insertRowsAtIndexPathsand associated methods.HTH,
Stretch