title: iOS UITableView number of rows in section using different array for same section
body: <p>So I have a UITableView with one section that I want to update on a button click. Now the number of rows in this section are getting populated using different arrays. I choose which one to return based on an index in a dictionary. Something like this:</p>
  
  <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  // Return the number of rows in the section.
  switch (section) {
      case 0:
          return 1;
  
      case 1: {
          NSMutableArray *array = [self.dict objectForKey:[NSNumber numberWithInteger:self.index]];
          return [array count] + 1;
      }
      default:
          break;
  }
  return 1;
  }
  </code></pre>
  
  <p>So from this, section 1's rows depend on different arrays. So now in my table view, I have a button that inserts another row in section 1 and I update the appropriate array as well. Now when I hit this other button that updates this section to show the values of a different array in the self.dict data structure I get an error:
  'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
  So for example, if in my first array I have 3 cells and then the user adds one more so there are 4 cells, then if the user clicks the button which should update the section 1 to show the second array (which has 3 cells), the error above is thrown. I think this is because the data source changed and the number of rows decreased without any explicit deletion of cells. How do I fix this problem?</p>
  
  <p>Updated with more code:</p>
  
  <pre><code>// Insert here when the very last cell of section 1 is selected
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSMutableArray *array = [self.dict objectForKey:[NSNumber numberWithInteger:self.index]];
      if (indexPath.section == 1 &amp;&amp; indexPath.row == [array count]) {
          [array addObject:[NSNumber numberWithInt:8]];
          [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[array count]-1 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
      }
  }
  
  
  // Code for reloading the table view
  
  - (IBAction)next:(id)sender {
      self.index++;
      [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
      [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
  }
  </code></pre>
  
  <p>}</p>