18

There is a similar question to this but answer is very general, vague.( Detecting UITableView scrolling ) Please don't dismiss. I am looking for concrete solution.

I have UITableView which has editable textfield and PickerView appears when another cell selected. What I need is to hide firstResponder or PickerView when user starts scrolling this UITableView.

So far in question Detecting UITableView scrolling there's a sugestion that you should subclass UITableView. IF you subsclass UITableView still internal/private UIScrollView is not accessible. How do I access UITableView's parent ScrollView (without breaking the law)?

Thanks.

Community
  • 1
  • 1
Rod
  • 1,905
  • 3
  • 18
  • 23

4 Answers4

42

You don't need to subclass UITableView to track scrolling. Your UITableViewDelegate can serve as UIScrollViewDelegate as well. So in your delegate class you can implement -scrollViewWillBeginDragging: or whatever UIScrollViewDelegate method you need in your situation. (as actually suggested in the question you mention)

Venk
  • 5,861
  • 8
  • 35
  • 49
Vladimir
  • 167,138
  • 35
  • 380
  • 310
  • 1
    Thanks, I got it. jus tneed to add to @interface MyTableViewController : UIViewController and then implement -scrollViewWillBeginDragging: Thanks. – Rod Feb 09 '10 at 07:51
  • How can you just get the scrolling from a specific table? Say for example I have 2 UITableViews? – SleepNot Apr 24 '14 at 06:01
  • 1
    @jeraldo, all UIScrollViewDelegate methods accept scroll view that calls it as parameter, so you can easily determine which UIScrollView you are dealing with – Vladimir Apr 24 '14 at 06:57
9

To expand on Vladimir's answer, this is how I implemented this solution:

In .h file:

@interface MyViewController : UIViewController <UIScrollViewDelegate> 

In .m file:

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {
    //logic here
}
bbeckford
  • 4,377
  • 4
  • 31
  • 47
8
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView == myTableView){
       // Your code here.....
    }
}
Tai Le
  • 6,003
  • 2
  • 35
  • 26
2

I had the same problem, and I got some ideas from the answers above to fix it, but not only the app crashes if I want to refresh while the table view is being scrolled, but also it crashes if I scroll while it's being refreshed. So the extended solution to fix the problem under all circumstances is to:


1.1. Disable scrolling if the user has pressed the refresh button

1.2. Enable scrolling once the refresh process is done


2.1. Disable the refresh button if the user is scrolling

2.2. Enable the refresh button once the user is finished scrolling


To implement the first part (1.1., and 1.2.):

-(void)startReloading:(id)sender
{
    ...
    self.tableView.userInteractionEnabled = NO;
    // and the rest of the method implementation
}

-(void)stopReloading:(id)sender
{
    self.tableView.userInteractionEnabled = YES;
    // and the rest of the method implementation
}

To implement the second part (2.1., and 2.2.):

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView
{
    barButton.enabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    barButton.enabled = YES;
}

And as it's explained in the previous answer, UISCrollViewDelegate needs to be set in the .h file:

@interface MyTableViewController : UITableViewController <UIScrollViewDelegate> 

P.S. You can use scrollEnabled instead of userInteractionEnabled, but it all depends on what you're doing, but userInteraction is the preferred option.

Neeku
  • 3,538
  • 8
  • 31
  • 42