title: Dynamically updating sections of UITableView without using reloadData
body: <p>First off I sincerely apologize if this has been answered. I have been battling with this for too long. I would appreciate any kind of help.</p>
  
  <p>I am developing an application that gets data asynchronously from different web sources and parses it into objects that are handled by my main Model. This model provides information for a UITableView. When data is received and parsed, i need to modify the number of sections and rows in the table view. I first receive the data that decides the number of sections, and then asynchronously data that modifies the number of rows in each section and their content. I have the table views data sources set up.</p>
  
  <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      // Return the number of sections.
  
      return [self.sijainti.pysakit count];
  
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      // Return the number of rows in the section.
      //if ([[[self.sijainti.pysakit objectAtIndex:section] ajatStr] count] == 0) return 1;
      NSLog(@"ajatStr of section: %d  count: %d",section, [[[self.sijainti.pysakit objectAtIndex:section] ajatStr] count]);
      return [[[self.sijainti.pysakit objectAtIndex:section] ajatStr] count];
  }
  </code></pre>
  
  <p>The object names are in Finnish (long story). The second line of numberOfRowsInSection was commented to ease debugging. Now, because the data is dynamic i need to update the interface when i have data ready for display, or the user wont know about it until they scroll away and back again. I have a method for this:</p>
  
  <pre><code>- (void)updateRowWithCell:(SCPysakki *)valmisPysakki {
      if (!valmisPysakki.isInArray) return;
  
      int section = [self.sijainti.pysakit indexOfObject:valmisPysakki];
  
      [self.tableView beginUpdates];
      NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:section];
      [self.tableView reloadSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
      [self.tableView endUpdates];
  }
  </code></pre>
  
  <p>This is called only after the number of sections is known. I do NOT want to use reloadData due to efficiency, and animation. I used to have this set up so that i had only rows and was just drawing rows inside of the rows. But once the rows became taller than the iphone screen, it became very unclear, because you couldn't see the title. So i made them sections, and the rows drawn real rows. Then i was suddenly faced with a problem. reloadRowsAtIndexPaths: withRowAnimation: worked absolutely fine but using reloadSections: withRowAnimation: gave me this exception:</p>
  
  <blockquote>
    <p><em>*</em> Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037</p>
    
    <p>2012-09-17 04:11:27.769 pysappi[2351:f803] <em>*</em> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  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 (0), 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).'</p>
  </blockquote>
  
  <p>I was able to eliminate this by adding the [self.tableView endUpdate] and [self.tableView endUpdate] to the update method. But oh no, that only works if I:</p>
  
  <ol>
  <li><p>Use reloadData after receiving the data deciding the number of sections (which in turn shortens the tableview so that after the whole update the user at the top of the table after every single update. I would rather update the sections dynamically, without using reloadData, except when removing Sections, but if I try to do this i get the exception again.)</p></li>
  <li><p>update the sections after receiving data for only one Row (so if I try to update all the rows in the section once I've received all their data, I get the exception again.)</p></li>
  </ol>
  
  <p>Now this creates a situation where creating a good user interface becomes impossible. I'm convinced that the underlying problem is not solved with the start and end updating calls, but that just fixes it for some very very odd reason.</p>
  
  <p>I also tried adding the rows manually with insertRowsAtIndexPaths: and updating them manually also, but this returns the exact same error. During a long debugging progress i have been able to determine that this exception rises after cellForRowAtIndexPath: gets called and it returns a cell for one of the new rows that was added during the updating, even though the numberOfRowsInSection should have updated the amount of rows. I also suppose that if those few circumstances apply this is why the start and end updating methods make the row updating work.</p>
  
  <p>The only thing that has worked outside of what i explained above, is using only reloadData, but that is just practically impossible considering the amount of updating.</p>
  
  <p>This became a lot longer than i thought, and its 4.30 AM. Not the first time I'm up this late thanks to this exception. If you need more information ask. Thank you in advance.</p>