28

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")
    }
}

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

3 Answers 3

58

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

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

2
  • @Rome Torres please accept as the correct answer if it helped Thanks. Commented Apr 7, 2017 at 3:08
  • How can I make this less sensitive for scrolling up? For instance, if I wanted to only cause an action if the tableview is scrolled upwards fast. Commented Aug 25, 2020 at 0:45
33

You can implement scrollViewWillEndDragging method:

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
    }

}
5
  • Thanks a lot that helped Commented Apr 7, 2017 at 3:17
  • 2
    This should be the answer!! Commented Sep 10, 2017 at 22:12
  • The cleanest solution
    – user6553825
    Commented Jan 8, 2018 at 16:17
  • Definitely this is the answer!
    – TibiaZ
    Commented Mar 8, 2018 at 12:03
  • 1
    Unfortunately, this variant doesn't work until you lift your finger Commented Dec 20, 2022 at 13:10
0

first we should make two property for last Offset and Current Offset

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

then in the Scrollview VC call the scrollViewWillBeginDragging for get current offset

 override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

        self.lastOffset = scrollView.contentOffset.y

    }

and scrollViewWillBeginDecelerating for get last offset

    override func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {

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

    }

then with this two variable you can know your scrollview W here it goes‍‍‍‍

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.