Search code examples
swiftuitableviewios9

Call api at specific scrollpoint while scrolling table view


I have one tableview and displaying products within custom cell.

There are so many records so i am using pagination right now but i want to call api for pagination when scroll point match with tableview cell scroll point before last 3 rows remains to scroll.

In short, I want to call api before last 3 rows is remains to scroll in table.

If there is other way to do like this then it will be appreciate.

Thank you in advance.


Solution

  • You can use the UITableView delegate method willDisplayCell:ForRowIndexPath:

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
        let needToFetchNewData = indexPath.row + 3 == datasource.count
    
        if needToFetchNewData {
            fetchData()
        }
    }
    

    I'd like to consider also a variable isAlreadyDownloading since the user can still scroll while you're fetching new data and it'll generate more than once the same request