I'm trying to have unlimited scroll in my app where i'm calling the data from an API, and at least it works to some extent, but when i scroll in the collection view, it starts populating below and it cuts of the ones on top so i can't scroll back up to them, then after populating it just skips and goes out of range, here's some code please help.
var fetchMore = false
var pageNumber = 0
var productPageString: String {
if pageNumber == 0 {
return "pageNumber=0&"
} else {
return "pageNumber=\(pageNumber)&"
}
}
func fetchProducts() {
fetchMore = false
let validatedTextString = UserDefaults.standard.object(forKey: "productsSearchValue")
let searchText = validatedTextString!
let urlString = APIConstants.baseurl + APIConstants.products + "?searchString=\(searchText)&\(productPageString)itemsPerPage=20"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
do {
let jsonData = try JSONDecoder().decode(ProductSearch.self, from: data!)
self.productSearchResults = [Products]()
for dictionary in jsonData.data {
let product = Products(productId: dictionary.productId, categoryId: dictionary.categoryId, storeId: dictionary.storeId, name: dictionary.name, description: dictionary.description, imageUrl: dictionary.imageUrl, price: dictionary.price)
self.productSearchResults?.append(product)
self.fetchMore = true
}
} catch let jsonError {
print(jsonError)
}
}.resume()
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
if offsetY > contentHeight - scrollView.frame.height {
if fetchMore {
fetchProducts()
pageNumber += 1
}
}
}
It initially shows the 20 items per page, but as the page number increases, it keeps populating with the other content in that particular page, but i can only still view 20 items per page. i want to be able to scroll back up to the previous ones and also, why does it end up taking me to the end automatically? Thanks
It works now, all i had to do was remove the line
self.productSearchResults = [Products]()
it wasn't necessary