iOS tableview 如何检查它是向上还是向下滚动 [英] iOS tableview how can I check if it is scrolling up or down

查看:69
本文介绍了iOS tableview 如何检查它是向上还是向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 TableView,我想知道如何确定 tableView 是向上还是向下滚动?我一直在尝试诸如此类的各种事情,但它没有奏效,因为下面是滚动视图并且我有一个 TableView .任何建议都会很棒,因为我是新手......

I am learning how to work with TableViews and I am wondering how can I figure out if the tableView is scrolling up or down ? I been trying various things such as this but it hasn't worked granted that below is for a scrollview and I have a TableView . Any suggestions would be great as I am new at this ...

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

这是我的 tableView 代码

This is what I have in my tableView code

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
    }

推荐答案

就像@maddy 在你的问题的评论中所说的,你可以使用 UIScrollViewDelegate 检查你的 UITableView 是否正在滚动 更进一步,您可以使用 scrollViewDidScrollscrollViewWillBeginDragging 函数来检查它滚动到哪个方向

Like @maddy said in the comment of your question, you can check if your UITableView is scrolling by using the UIScrollViewDelegate and further more you could check which direction it scrolls to by using both scrollViewDidScroll and scrollViewWillBeginDragging functions

// 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
    }
}

此外:如果您已经对 UIViewController 进行了子类化,则无需使用 UIScrollViewDelegateUIViewController 进行子类化code> 和 UITableViewDelegate 因为 UITableViewDelegate 已经是 UIScrollViewDelegate

Furthermore: You don't need to subclass your UIViewController with UIScrollViewDelegate if you've already subclassed your UIViewController with UITableViewDelegate because UITableViewDelegate is already a subclass of UIScrollViewDelegate

这篇关于iOS tableview 如何检查它是向上还是向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆