Reputation: 125
I am using pagination to show manage multiple data. Pagination works on both sides top and bottom. For this, I am using below code to call API. But I have faced the issue when data is not greater then tableView
height. In this case scrollViewDidEndDragging
method not called. So please tell me how to solve this problem. below code is working fine when data is greater then tableView
height.
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0) {
print("up")
if workerInfo.count > 0 {
let topVisibleIndexPath:IndexPath = self.tableView.indexPathsForVisibleRows![0]
if topVisibleIndexPath.row == 0 && startCount != 0 && !isDataLoading {
isDataLoading = true
startCount = startCount - requiredCount
self.callAPI(isCallFromPagination: true)
}
}
}
else {
print("down")
if workerInfo.count > 0 {
let arrayOfVisibleItems = tableView.indexPathsForVisibleRows?.sorted()
let lastIndexPath = arrayOfVisibleItems!.last
// print("Array: ", arrayOfVisibleItems)
print("Last IndexPath: ", lastIndexPath as Any)
if lastIndexPath?.row == workerInfo.count - 1 && !isDataLoading {
isDataLoading = true
startCount = startCount + requiredCount
self.callAPI(isCallFromPagination: true)
}
}
}
}
Upvotes: 2
Views: 2502
Reputation: 90
Can you please check this properties.In my code its works perfectly.
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("Called")
}
}
Upvotes: 2
Reputation: 439
// Support Pagination
extension TableViewController: UIScrollViewDelegate {
// Set Pagination Trigger before DataSoruce remaining 6 items display
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Load More
setupPaginationAt(indexPath)
}
// If we reached at the end, check again if anything to load
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
if (bottomEdge >= scrollView.contentSize.height) {
// We are at the end
setupPaginationAt(nil)
}
}
func setupPaginationAt(_ indexPath: IndexPath?) {
// If any network calls on the way, we must leave - Declare a Bool variable and manipulate the value of it
if isLoading {
return
}
// Validation
guard let _dataSource = YourDataSource else {
print("DataSource found empty")
return
}
// Config Pagination Call
func execute() {
// Get Current Pagination - Hope you have page index's from API
if let currentPage = dataSource.pageIndex, let lastPage = dataSource.totalPages {
if currentPage < lastPage {
let nextPage = currentPage + 1
loadData(at: nextPage)
}
}
}
// Check InBetween or End
if let _indexPath = indexPath {
if _indexPath.row == _dataSource.count - 6 {
execute()
}
} else {
// Assume End
execute()
}
}
}
This solution should work.
Upvotes: 1
Reputation: 90
You can keep your table view
bounce
and scrollview
bounce True
. then scrollViewDidEndDragging method called
Upvotes: 0