Swift, unable to update table view with more rows
<p><strong>Goal:</strong> I have a list of Items fetched from server that are displayed in a TableView, the items are divided in 20s. First, I load 20 items, then when scroll reach bottom I need to add the next 20 items.</p>

<p><strong>Issue</strong>:When I run the update table view I get error </p>

<blockquote>
  <p>The number of rows contained in an existing section after the update (40) must be equal to the number of rows contained in that section before the update (20), plus or minus the number of rows inserted or deleted from that section (1 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>updating tableview Datasource functions is not working with me, here is the code:</p>

<p><strong>Detect scroll when it reach bottom</strong> </p>

<pre><code>func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    let offset = scrollView.contentOffset
    let bounds = scrollView.bounds
    let size = scrollView.contentSize
    let inset = scrollView.contentInset
    let y = CGFloat(offset.y + bounds.size.height - inset.bottom)
    let h = CGFloat(size.height)

    let reload_distance = CGFloat(-100)

      if (y &gt; (h + reload_distance)) {

            pageNum += 1
            manager.getNewItems(String(pageNum)) //call the JSON
      }
}
</code></pre>

<p>TableView update code</p>

<pre><code>//json response function    
func didReceiveResponse(info: [String : AnyObject]){

    // get the data and set in arrays (e.g. names[])
    // Then I reload my table and show first 20 Items MyTable.reload()
    // Working fine until here

   if pageNum &gt; 0{

     //names.count is 40 now

     self.MyTable.beginUpdates()

                                             //I tried names.count-20 here
     tableView(MyTable, numberOfRowsInSection: (names.count))
               MyTable.insertRowsAtIndexPaths([
               NSIndexPath(forRow: (names.count)-20, inSection: 0)
               ], withRowAnimation: .Automatic)


            MyTable.endUpdates()
     }
}  
</code></pre>

<p>of course loading first 20 is perfectly working, the scroll perfectly call the function, everything work only the update fails.</p>

<p>I am puzzling with this riddle for 4 day, couldn't find any working solutions online. Your help is really needed guys :)</p>

<p>thanks</p>

<p>Edit:</p>

<p>This is how I fill the data in tableView</p>

<pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

    let MyReturnCell = MyTable.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomCell

    MyReturnCell.name!.text = names[indexPath.row]
    //and so on until I fill all the data inside the MyCell

           return MyReturnCell
</code></pre>

<p>}</p>