title: UITableView deleteRowsAtIndexPath when to update the model?
body: <p>I'm implementing a method to remove a row from my tableview based on a domain object but I'm getting a error after call deleteRowsAtIndexPath:animated</p>
  
  <blockquote>
    <p>Blockquote Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'</p>
  </blockquote>
  
  <p>So I thought "ok, I'm updating the model too soon". The code:</p>
  
  <pre><code>    - (void)deleteItem:(Item *)item {
      NSInteger index = [items indexOfObject:item]; // items is the model a NSArray
      if (index != NSNotFound) {
          NSMutableArray *itemEditable = [items mutableCopy];
          [itemEditable removeObjectAtIndex:index];
          self.items = itemEditable; 
  
          NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:index inSection:0];
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationMiddle];
      }
  }
  </code></pre>
  
  <p>And then I change the code to update the model <em>after</em> I call deleteRowsAtIndexPath:animated</p>
  
  <pre><code>    - (void)deleteItem:(Item *)item {
      NSInteger index = [items indexOfObject:item]; // items is the model a NSArray
      if (index != NSNotFound) {
          NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:index inSection:0];
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationMiddle];
  
          NSMutableArray *itemEditable = [items mutableCopy];
          [itemEditable removeObjectAtIndex:index];
          self.items = itemEditable; 
  
      }
  }
  </code></pre>
  
  <p>But it didn't solve, the only thing that changes is the number of rows in the error message from 2 to 3.</p>
  
  <blockquote>
    <p>Blockquote Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  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 (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'</p>
  </blockquote>
  
  <p>Where should I update the model?</p>
  
  <hr>
  
  <p><strong>Added numberOfRowsInSection method</strong></p>
  
  <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return [items count];
  }
  </code></pre>