The use of UIRefreshControl in iOS development

Problem description

On the next topic, after TabBar click refresh is implemented, continue to write to complete the function and refresh UITableView. Considering that after iOS 10, UIScrollView already has the property of UIRefreshControl, simply write with its own. So you have the following code:

  1. Add UIRefreshControl to UITableView
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    
refreshControl.tintColor = [UIColor grayColor];
    
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Drop-down refresh"];
    
[refreshControl addTarget:self action:@selector(refreshTabView) forControlEvents:UIControlEventValueChanged];

self.newsTableView.refreshControl = refreshControl;

  1. Drop down refresh event
-(void)refreshTabView
{
    //Add a piece of data
    [self.newsData insertObject:[self.newsData firstObject] atIndex:0];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.newsTableView reloadData];
        
        if ([self.newsTableView.refreshControl isRefreshing]) {
            
            [self.newsTableView.refreshControl endRefreshing];
        }
    });
}
  1. TabBar click event
-(void)doubleClickTab:(NSNotification *)notification{
    
    //There's a hole here. If you use NSInteger directly, there will be a problem. The number is wrong
    //Because the last interface is encapsulated as an object when it is transmitted, it is received with NSNumber and then retrieved
    NSNumber *index = notification.object;
    
    if ([index intValue] == 1) {
        //Refresh
        [self.newsTableView.refreshControl beginRefreshing];
        
    }
    
}

At this time, the effect is as follows. You can directly pull down the refresh button, but you can't click TabBar:

Refresh exception.gif

Analysis problem

After Google's help, we finally know the reason, because the UIRefreshControl system has two traps:

  1. Calling the - beginRefreshing method will not trigger the UIControlEventValueChanged event;
  2. Calling the - beginRefreshing method does not automatically display the progress circle.

In other words, just calling the - beginRefreshing method doesn't matter, so two things need to be done:

  1. Set the event of UIRefreshControl manually;
  2. Manually set the content offset of UITableView to expose the progress circle.

Solve the problem

Just modify the code in step 3 above as follows:

-(void)doubleClickTab:(NSNotification *)notification{

    //There's a hole here. If you use NSInteger directly, there will be a problem. The number is wrong
    //Because the last interface is encapsulated as an object when it is transmitted, it is received with NSNumber and then retrieved
    NSNumber *index = notification.object;
    
    if ([index intValue] == 1) {
        //Refresh
        //animated don't be YES, or the chrysanthemum will be stuck
        [self.newsTableView setContentOffset:CGPointMake(0, self.newsTableView.contentOffset.y - self.newsTableView.refreshControl.frame.size.height) animated:NO];
        
        [self.newsTableView.refreshControl beginRefreshing];
        
        [self.newsTableView.refreshControl sendActionsForControlEvents:UIControlEventValueChanged];
    }
    
}

Final effect:


Refresh normal.gif

Tags: iOS Google

Posted on Thu, 02 Apr 2020 17:37:43 -0700 by Tim L