How to detect scrolling of UITableView?

前端 未结 4 1901
无人共我
无人共我 2021-02-04 05:05

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 ha

相关标签:
4条回答
  • 2021-02-04 05:30
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        if (scrollView == myTableView){
           // Your code here.....
        }
    }
    
    0 讨论(0)
  • 2021-02-04 05:32

    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.

    0 讨论(0)
  • 2021-02-04 05:36

    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
    }
    
    0 讨论(0)
  • 2021-02-04 05:40

    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)

    0 讨论(0)
提交回复
热议问题