How to detect scrolling of UITableView?

前端 未结 4 1911
无人共我
无人共我 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: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  
    

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

提交回复
热议问题