I have a TableViewController and the news is displayed there. I need every when the scrolling reaches the end to download news from the API with pagination.

First I upload the https://****.**/api/posts which equals page 1.

Then I need to load page 2 like this: https://****.**/api/posts?page=2

How to change the page number each time?

 let downloader = Downloader() var page = 2 override func scrollViewDidScroll(_ scrollView: UIScrollView) { let url = "https://****.**/api/posts?page=\(page))" if (scrollView.contentOffset.y > self.lastOffsetY) { downloader.getLastestNews(url) } } override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { lastOffsetY = scrollView.contentOffset.y } 

    1 answer 1

    As I do, firstly a request to the server includes a post or get request with the page parameter and, depending on the page, respectively, returns data for example page = 1 returns 20, then page = 2 returns from 20 to 40, and so on. I have a method that makes a request to the server and parses the data.

    startDownload(page: Bool, isNew: False)

    you can have it different.

    UITableViewDataSource has a method

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)

    there I process the number of cells and then the load:

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.row == self.goodsItems.count - 2 && !self.flagOfEmptyMas && !self.flagForDownloadingMas { startDownload(page: true, isNew: false) } } 

    if the current cell = (the number of array elements is 2, i.e. 18) then load the data, already from page2 and so on.

    I advise you to put the twister at the bottom of the table

     self.myTableView.tableFooterView = self.spinner 

    Good luck.

    • No, the question was how to change the page every time - Zhan