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
3 Answers
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()
}
}

- 334
- 4
- 17
-
it works fine but how if i scroll above, data not load while scrolling above – IOS Lerner Feb 12 '20 at 06:06
-
Check this [answer](https://stackoverflow.com/a/35213858/5025731) – Hassan ElDesouky Feb 12 '20 at 08:47
You should implement the UITableViewDataSourcePrefetching
protocol. This protocol will help you to fill seamlessly a table by new data

- 1,067
- 1
- 16
- 26
-
-
You can see an example of the implementation of this protocol [here](https://github.com/rokonuddin/TableViewPrefetch) – Emin A. Alekperov Feb 12 '20 at 16:53
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

- 46
- 5