Why does my UITableView "jump" when inserting or removing a row? : <p>(Happy to accept an answer in Swift or Objective-C)</p>

<p>My table view has a few sections, and when a button is pressed, I want to insert a row at the end of section 0.  Pressing the button again, I want delete that same row.  My almost working code looks like this:</p>

<pre><code>// model is an array of mutable arrays, one for each section

- (void)pressedAddRemove:(id)sender {
    self.adding = !self.adding;  // this is a BOOL property
    self.navigationItem.rightBarButtonItem.title = (self.adding)? @"Remove" : @"Add";

    // if adding, add an object to the end of section 0
    // tell the table view to insert at that index path

    [self.tableView beginUpdates];
    NSMutableArray *sectionArray = self.model[0];
    if (self.adding) {
        NSIndexPath *insertionPath = [NSIndexPath indexPathForRow:sectionArray.count inSection:0];
        [sectionArray addObject:@{}];
        [self.tableView insertRowsAtIndexPaths:@[insertionPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    // if removing, remove the object from the end of section 0
    // tell the table view to remove at that index path

    } else {
        NSIndexPath *removalPath = [NSIndexPath indexPathForRow:sectionArray.count-1 inSection:0];
        [sectionArray removeObject:[sectionArray lastObject]];
        [self.tableView deleteRowsAtIndexPaths:@[removalPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [self.tableView endUpdates];
}
</code></pre>

<p>This behaves properly sometimes, but sometimes not, depending on where the table view is scrolled:</p>

<ul>
<li>Section 0 at the very top, contentOffset.y == 0:  Works great, the row is inserted and the stuff below section 0 animates downward</li>
<li>Section 0 invisible, because the table is scrolled past it:  Works great, the visible content below the new row animates downward as if a row was inserted above it.</li>
<li>BUT: if the table view is scrolled a little, so that part of section 0 is visible:  it works wrong.  In a single frame, all of the content in the table view jumps up (content offset increases) Then, with animation, the new row gets inserted and the table view  content scrolls back down (content offset decreases).  Everything ends up where it should be, but the process looks super bad with that single frame "jump" at the start.</li>
</ul>

<p>I can see this happen in slow-motion the simulator with "Debug->Toggle Slow Animations".  The same problem occurs in reverse on the deletion.</p>

<p>I've found that the size of the jump in offset is related to the how far into section 0 the table is scrolled:  the jump tiny when the offset is tiny.  The jump gets bigger as the scrolling approaches <em>half</em> of section 0 total height (the problem is at it's worst here, jump == half the section height).  Scrolling further, the jump gets smaller.  When the table is scrolled so that only a tiny amount of section 0 is still visible, the jump is tiny.</p>

<p><strong>Can you help me understand why this is and how to fix?</strong></p>
