I'm trying to create an app where the user scrolls to the bottom of the tableview more results are displayed. At present only 20 items are displayed as per the API. The API has a parameter for page which I'm trying to increment by 1 each time the user scrolls to the bottom via this in the ViewController.
When I open the app it loads me page 1, when I go to the bottom of the tableView it quickly loads me in sequence page 2, 3, 4, 5 and stops at page 5. All the other pages are not displayed anymore, only page 5 is seen.
How can I see all the pages together after scrolling down?
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
swiftManager.delegate = self
swiftManager.fetchUrl()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
if (offsetY > contentHeight - scrollView.frame.height){
swiftManager.fetchUrlLoadMore()
}
}
SwiftManager.swift
struct SwiftManager {
let baseURL = "https://api.themoviedb.org/3/movie/popular"
let apiKey = "xxxxxxxxxxxxx"
let filterURL1 = "language=en-US"
let filterURL2 = "sort_by=popularity.desc"
let filterURL3 = "page="
var filterPage = 1
func fetchUrl() {
let urlString = "\(baseURL)?api_key=\(apiKey)&\(filterURL1)&\(filterURL2)&\(filterURL3)\(filterPage)"
performRequest(with: urlString)
print("Number page = \(filterPage)")
}
mutating func fetchUrlLoadMore() {
while (filterPage<5) {
let urlString = "\(baseURL)?api_key=\(apiKey)&\(filterURL1)&\(filterURL2)&\(filterURL3)\(filterPage)"
performRequest(with: urlString)
filterPage += 1
print("Number page = \(filterPage)")
}
}