title: Problems with simultaneous insertion/deletion of rows in UITableView
body: <p>I've been struggling with this issue for a couple of days now. I need to simultaneously delete and insert some rows in an UITableView inside a beginUpdates/endUpdates block, but some of the inserted rows have an index that surpasses the original table view element count. I took sample code from Apple documentation on batch insertion and deletion and modified it in order to cause the same problem, here is the code:</p>
  
  <pre><code>#import "MasterViewController.h"
  
  @implementation MasterViewController
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          self.title = NSLocalizedString(@"Master", @"Master");
          states = [[NSMutableArray arrayWithObjects:@"Arizona", @"California", @"New Jersey", @"Washington", nil] retain];
      }
      return self;
  }
  
  - (void)dealloc
  {
      [states release];
      [super dealloc];
  }
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      return [states count];
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      }
  
      cell.textLabel.text = [states objectAtIndex:indexPath.row];
      return cell;
  }
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      [states removeObjectAtIndex:0];
      [states removeObjectAtIndex:0];
      [states removeObjectAtIndex:0];
  
      [states insertObject:@"Alaska" atIndex:1];
      [states insertObject:@"Georgia" atIndex:1];
      [states insertObject:@"Wisconsin" atIndex:1];
  
      NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
                               [NSIndexPath indexPathForRow:0 inSection:0],
                               [NSIndexPath indexPathForRow:1 inSection:0],
                               [NSIndexPath indexPathForRow:2 inSection:0],
                               nil];
  
      NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                               [NSIndexPath indexPathForRow:2 inSection:0],
                               [NSIndexPath indexPathForRow:3 inSection:0],
                               [NSIndexPath indexPathForRow:4 inSection:0],
                               nil];
  
      UITableView *tv = (UITableView *)self.view;
  
      [tv beginUpdates];
      [tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
      [tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationTop];
      [tv endUpdates];
  }
  
  @end
  </code></pre>
  
  <p>When I run this and tap any row, I get the following exception:</p>
  
  <pre><code>*** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewSupport.m:386
  Uncaught exception: Invalid table view update.  The application has requested an update to the table view that is inconsistent with the state provided by the data source.
  </code></pre>
  
  <p>Is there something fundamentally wrong with this code that I'm not seeing?</p>