我正在使用分页来显示管理多个数据。分页适用于顶部和底部的两侧。为此,我使用下面的代码来调用API。但是当数据不大于tableView
高度时,我就遇到了问题。在这种情况下,不会调用scrollViewDidEndDragging
方法。所以请告诉我如何解决这个问题。当数据大于tableView
高度时,下面的代码可以正常工作。
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)
}
}
}
}
答案 0 :(得分:1)
可以请您检查一下此属性。在我的代码中,它可以正常工作。
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("Called")
}
}
答案 1 :(得分:0)
您可以保持table view
bounce
和scrollview
弹跳True
。然后使用scrollViewDidEndDragging方法
答案 2 :(得分:0)
// 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()
}
}
}
此解决方案应该有效。