Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:55:32+00:00 2026-06-06T03:55:32+00:00

Could somebody explain what I’m doing wrong in attempting to insert a new UITableViewCell?

  • 0

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];
    }     
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:55:34+00:00Added an answer on June 6, 2026 at 3:55 am

    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)section

    I 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:

    1. Your call to insertRowsAtIndexPaths is requesting that the Controller update the View with an additional row.
    2. The Controller then does a sanity check to see if you have your sums right, so it calls:
      tableView:numberOfRowsInSection

      Now 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

    3. 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 when tableView:numberOfRowsInSection is 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 reloadData anymore, instead the insert/reload is wrapped in begin/end update calls – The animated updates will occur after endUpdates due to the reloadSections call.

    NSMutableIndexSet *sectionsToReload = [[NSMutableIndexSet alloc] init];
    [sectionsToReload addIndex:1]; // Add sections as necessary
    
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView reloadSections:sectionsToReload withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
    

    Check out the WWDC 2010 video from Apple, “Mastering Table Views” – explains everything really nicely, regarding using insertRowsAtIndexPaths and associated methods.

    HTH,
    Stretch

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could somebody explain to me why the following is wrong : $sql='SELECT * FROM
In my Javascript code += increments the number but ++ doesn't. Could somebody explain
Could somebody explain how the following example works? I don't understand how this can
Could somebody explain how this app (FoodSpotting), creates their custom section header? It has
Good day, everyone. Could somebody explain why following code does work in Google Chrome
Could somebody explain to me, with the help of examples, what is cardinality in
Could somebody explain what is the meaning of DomHelper in google closure? What is
Could somebody explain to me the architecture behind chatroulette? I was thinking about a
Could somebody explain me how to map a one-to-zero association with hibernate (using mapping
Could somebody please provide the code to do the following: Assume there is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.