Scroll on Demand keeps calling

时间:2018-06-20 05:04:23

标签: ios swift uitableview pagination

I am trying to implement scroll on demand call. Until I scroll at the bottom of the screen, it never calls doPaging() method which is good and then downloads another batch of dataset( 20 more items). However when it reaches at the bottom of screen first time and keeps calling even small scroll to the bottom.

I wonder what I am missing in the following implementation.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{   
    if indexPath.row == self.products.count - 1 && !isWating {
        isWating = true
        self.pageNumber += 1
        self.doPaging()
    }
}

func doPaging() 
{
    fetchProducts(page: pageNumber , completion: { success in
        if let products = success as? Products
        {
           // keeps calling
            DispatchQueue.main.async {
                self.products.append(contentsOf:products)
                self.isWating = false;
                self.productTableView.reloadData()
            }
        }
    })
}

1 个答案:

答案 0 :(得分:0)

使用此方法。这一定可以工作

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    //Bottom Refresh
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) {
        if !isWating {
            isWating = true
            self.doPaging()
        }
    }
}