Engineeroholic Engineeroholic - 3 months ago 12
Swift Question

Swift, unable to update table view with more rows

Goal: 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.

Issue:When I run the update table view I get error

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).'

updating tableview Datasource functions is not working with me, here is the code:

Detect scroll when it reach bottom

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 > (h + reload_distance)) {

pageNum += 1
manager.getNewItems(String(pageNum)) //call the JSON

TableView update 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 > 0{

//names.count is 40 now


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


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

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



This is how I fill the data in tableView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let MyReturnCell = MyTable.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomCell!.text = names[indexPath.row]
//and so on until I fill all the data inside the MyCell

return MyReturnCell



Following code example may help you understand what you need to do:

  1. Create an array of index paths
  2. Add the 20 data objects which you received from the server in your data source.
  3. Now since your data source and array of index paths are on the same page, begin table view updates and insert the rows.

That's it. :)

func insertRows() {
            // build the index paths for insertion
            // since you're adding to the end of datasource, the new rows will start at count
        var indexPaths: [NSIndexPath] = [NSIndexPath]()
        var currentCount: Int = self.names.count
        for i in 0..<20 {//hardcoded 20, this should be the number of new results received
            indexPaths.append(NSIndexPath(forRow: currentCount + i, inSection: 0))
        // tell the table view to update (at all of the inserted index paths)
        self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Top)