1

Basically i have a table view and get 5 item in every time when i call the api. So how to use/ reload more data white scrolling table view. array.count-1 not for me at cell for row indexpath because when device size bigger all data shown at a time and api not call

IOS Lerner
  • 11
  • 3
  • You should trigged your call in `tableViewWillDisplayCell`. Every time the indexPath is reaching the last of your local count, fetch the API – CZ54 Feb 10 '20 at 15:24

3 Answers3

0

You should be sending the page number in the API request.

First: declare the variable currentPage with initial value 0 and a boolean to check if any list is being loaded with initial value false, it's to prevent the scroll view from getting more lists/items in one scroll to the bottom of the tableView.

var currentPage = 0
var isLoadingList = false

Second: This is the function that fetches the data:

func getFromServer(_ pageNumber: Int){
     self.isloadingList = false
     self.table.reloadData()
} 

Third: Implement the function that increments the page number and calls the API function on the current page.

func loadMoreItems(){
     currentPage += 1
     getFromServer(currentPage)
}

Fourth: When the scrollView scrolls you should get the other items/lists from the API.

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) && !isLoadingList){
            self.isLoadingList = true
            self.loadMoreItems()
        }
    }
Hassan ElDesouky
  • 334
  • 4
  • 17
0

You should implement the UITableViewDataSourcePrefetching protocol. This protocol will help you to fill seamlessly a table by new data

Emin A. Alekperov
  • 1,067
  • 1
  • 16
  • 26
0

Declare

current page = 1

Then add this method in viewController

private func isLastCell(indexPath: IndexPath) -> Bool {
    print(indexPath)
    if array.count > 0 {
        return ((indexPath.section == sectionCount-1) && (indexPath.row == (array.count-1)))
    } else {
        return false
    }

}

After that in tableViewWillDisplayCell method add this function

if array.count > 0, self.isLastCell(indexPath: indexPath) {
            self.currentPage += 1
            // call api here
  } 

It works for me