scrollViewDidEndDragging не вызывается в UITableview, когда данных меньше - PullRequest
0 голосов
/ 15 октября 2018

Я использую нумерацию страниц, чтобы показать управление несколькими данными.Разбивка на страницы работает с обеих сторон сверху и снизу.Для этого я использую приведенный ниже код для вызова 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)
             }
         }
    }
}

Ответы [ 3 ]

0 голосов
/ 15 октября 2018
// 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()
        }
    }
}

Это решение должно работать.

0 голосов
/ 15 октября 2018

enter image description here

Можете ли вы проверить эти свойства. В моем коде все работает отлично.

extension ViewController: UIScrollViewDelegate {

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("Called")
   }
  }
0 голосов
/ 15 октября 2018

Вы можете сохранить свои table view bounce и scrollview отказов True.затем метод scrollViewDidEndDragging с именем

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...