1

I have a table view in my iPhone app

in that i want to implement two actions

  1. when user scrolls the table to top.

  2. When the user pulls down the table view.

For Ex:

If(UserScrollThe tableViewToTop)
{
 perform action: 1
}

If(UserScrollThe tableViewToDown)
{
 perform action: 2
}

How can we know that the use move the table to Top / bottom..?

iOS dev
  • 2,254
  • 6
  • 33
  • 56

2 Answers2

3

Check using scrollView's delegate method.

1) Set yourtableView.scrollView.delegate = self;

2) Add UIScrollViewDelegate in .h file


Add CGPoint previousContentOffset in .h

 (void)scrollViewDidScroll:(UIScrollView *)scrollView 
 {
   if (scrollView.contentOffset.y > previousContentOffset.y) {
       //scrolled downward
       //perform action for scrolled downward
   }
   else
   {
       //scrolled upward
       //perform action for scrolled upward
   }

   previousContentOffset = scrollView.contentOffset
 }
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
1

You can conform to the UIScrollViewDelegate protocol and implement the following methods:

  1. For scrolling to top, use scrollViewDidScrollToTop:
  2. For scrolling down, you can implement scrollViewDidScroll:, record the content offset, and compare it with the previous content offset. For example:

    - (void)scrollViewDidScroll:(UIScrollView*)scrollView {

    if (scrollView.contentOffset.y > self.previousContentOffset.y) {
        //This was a scroll down - do something
     }
    } 
    

For #2, check to make sure it's called when you want. Sometimes, depending on what you're using it for, you might also want to implement scrollViewDidEndDragging:willDecelerate and/or scrollViewDidEndScrollingAnimation: since the scroll view calls different delegate methods if the user scrolls and stop manually (by stopping their finger) or if the user scrolls and lets go, allowing the scroll view to keep scrolling till it stops on its own.

Jai Govindani
  • 3,181
  • 21
  • 26