title: UITableView crash when updating cells that change sections
body: <p>I have a table that displays data from a single array and organizes the items into separate sections based on a set of filters. Each filter has a corresponding section. My UI allows the user to tap a check box embedded in each table cell. This check box sets a "checked" flag which affects which section the item is filtered to. Here's an example:</p>
  
  <ul>
  <li>Section 1
  
  <ul>
  <li>Item A</li>
  <li>Item B</li>
  </ul></li>
  <li>Section 2</li>
  <li>Section "Done"</li>
  </ul>
  
  <p>Tapping the check box next to Item A should cause the table to rearrange to look like this:</p>
  
  <ul>
  <li>Section 1
  
  <ul>
  <li>Item B</li>
  </ul></li>
  <li>Section 2</li>
  <li>Section "Done"
  
  <ul>
  <li>Item A</li>
  </ul></li>
  </ul>
  
  <p>Below is my code for responding to the check box tap. It figures out which row the check box belongs to and then attempts to create an animation block that deletes the row from its old section and adds a row to the end of the "Done" section. Please keep in mind that the array doesn't actually change -- the way the data is filtered into the sections changes based on the value of the items' checked properties.</p>
  
  <pre><code>- (IBAction)checkBoxTapped:(id)sender
  {
   // Get the table cell
   CheckBoxControl* checkBox = (CheckBoxControl*)sender;
  
   UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
   UITableView* table = (UITableView*)[cell superview];
   NSIndexPath* indexPath = [table indexPathForCell:cell];
  
   Item* item = [self itemAtRow:[indexPath row] inSection:[indexPath section]];
   item.checked = checkBox.checked;
  
   Filter* filter = [filters objectAtIndex:DONE_INDEX];
   // We want the new row at the bottom of the "Done" section.
   NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:[self countItemsWithFilter:filter] inSection:DONE_INDEX];
  
   [table beginUpdates];
   [table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
   [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   [table endUpdates];
  }
  </code></pre>
  
  <p>This code always throws an exception:</p>
  
  <pre><code>2010-01-13 00:19:44.802 MyApp[19923:207] *** Terminating app due to uncaught
  exception 'NSRangeException', reason:
  '*** -[NSMutableIndexSet addIndexesInRange:]:Range {2147483647, 1}
  exceeds maximum index value of NSNotFound - 1'
  </code></pre>
  
  <p>Can anyone help me figure out what is causing the NSRangeException? Here's my stack:</p>
  
  <pre><code>___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___
  obj_exception_throw
  +[NSException raise:format:arguments:]
  +[NSException raise:format:]
  -[NSMutableIndexSet addIndexesInRange:]
  -[NSMutableIndexSet addIndex:]
  -[_UITableViewUpdateSupport(Private) _computeRowUpdates]
  -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:oldRowRange:newRowRange:context:]
  -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:]
  -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:]
  -[UITableView endUpdates]
  -[RootViewController checkBoxTapped:]
  </code></pre>
  
  <p>If this code is just plain wrong then how should I animate removing a row from one section and adding it to another?</p>
  
  <p>Thanks in advance for any help you can give me.</p>
  
  <p>-Mike-</p>