iOS的tableview如何检查它是向上滚动还是向下滚动?

28

我正在学习如何使用TableView,想知道如何判断tableView是向上滚动还是向下滚动?我尝试了一些方法,比如这个方法,但它不起作用,因为它是针对ScrollView的,而我有一个TableView。由于我是新手,所以希望得到一些建议...

  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        print("down")
    } else {
        print("up")
    }
}

这是我在tableView代码中拥有的内容

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
        return Locations.count
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == self.Posts.count - 4 {

            reloadTable(latmin: self.latmin,latmax: self.latmax,lonmin: self.lonmin,lonmax: self.lonmax,my_id: myID)
            print("Load More")
        }

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! NewCell


   cell.post.text = Posts[indexPath.row]
   cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)

        return cell
    }

2
一个 UITableView 是一个 UIScrollView。所有的 UIScrollViewDelegate 方法都适用于表视图。 - rmaddy
3个回答

58

像 @maddy 在您的问题评论中所说的那样,您可以通过使用 UIScrollViewDelegate 来检查您的 UITableView 是否正在滚动,并且您还可以通过同时使用 scrollViewDidScrollscrollViewWillBeginDragging 函数来检查它滚动的方向。

// we set a variable to hold the contentOffSet before scroll view scrolls
var lastContentOffset: CGFloat = 0

// this delegate is called when the scrollView (i.e your UITableView) will start scrolling
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = scrollView.contentOffset.y
}

// while scrolling this delegate is being called so you may now check which direction your scrollView is being scrolled to
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if self.lastContentOffset < scrollView.contentOffset.y {
        // did move up
    } else if self.lastContentOffset > scrollView.contentOffset.y {
        // did move down
    } else {
        // didn't move
    }
}

此外:如果您已经使用UITableViewDelegate子类化了UIViewController,则不需要再使用UIScrollViewDelegate对其进行子类化,因为UITableViewDelegate已经是UIScrollViewDelegate的子类。


@Rome Torres 如果这个回答有帮助,请接受为正确答案。谢谢。 - Zonily Jame
如何减轻向上滚动的灵敏度?例如,如果我只想在快速向上滚动UITableView时才触发某个操作。 - helloworld12345

33

您可以实现scrollViewWillEndDragging方法:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {


    if targetContentOffset.pointee.y < scrollView.contentOffset.y {
        // it's going up
    } else {
        // it's going down
    }

}

非常感谢,这很有帮助。 - Rome Torres
2
这应该是答案!! - Bryan Norden
最干净的解决方案 - user6553825
绝对是正确的答案! - TibiaZ
1
很遗憾,除非你抬起手指,否则这个变量不起作用。 - Ilya Biltuev

0

首先我们应该为"最后偏移量"和"当前偏移量"分别创建两个属性。

 var lastOffset = CGFloat()
 var currentOffset = CGFloat()

然后在ScrollView VC中调用scrollViewWillBeginDragging以获取当前偏移量

 override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

        self.lastOffset = scrollView.contentOffset.y

    }

并使用scrollViewWillBeginDecelerating获取最后的偏移量

    override func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {

        self.lastOffset = scrollView.contentOffset.y
        viewStatus(scrollView: scrollView)

    }

有了这两个变量,你就可以知道你的滚动视图去哪里了。

‍‍‍let isScrollUp = scrollView.contentOffset.y > self.lastOffset
let isScrollDown  = scrollView.contentOffset.y < self.lastOffset

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接