Here I am having layout in which one of my table view cell consists of collection view and in this I need to implement pagination and I am unable to use func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
this function now how to implement pagination for table view can anyone help me how to implement this to place condition when it reaches last element of my collection view and here I know how to implement pagination but how to call the function which needs to reload ?
Here is my function which used to detect the last cell and make to reload the data by using below function
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let obj = items
let lastElement = (obj.count) - 1
print(lastElement)
if !loadingData && indexPath.row == lastElement {
loadingData = true
guard let obj = self.listClassModel else { return }
if ((obj.searchCriteria.pageSize as? Int) != nil) {
self.collectionView.bottomRefreshControl?.beginRefreshing()
let viewController = HomeViewController()
viewController.loadMoreData()
}
}
func loadMoreData() {
DispatchQueue.global(qos: .background).async {
guard let obj = self.listClassModel else { return }
let totalItems = obj.totalCount
let numberOfItems = obj.searchCriteria.pageSize as! Int
let float = Float(totalItems)/Float(numberOfItems)
print(float)
let totalPages = Int(ceil(float))
print(self.count)
if totalPages != self.count {
self.index += 1
self.count += 1
print(self.count)
let batchSize = 10
let sortField = "name"
let sortOrder = "ASC"
let conditionType = "eq"
let categoryId = 1
self.listCategoryDownloadJsonWithURL(listUrl: listUrlWithParameters)
}
else {
self.loadingData = false
}
DispatchQueue.main.async {
// this runs on the main queue
self.loadingData = false
}
}
}