0
votes

I have implemented refreshControl as shown below in viewDidLoad():

refreshControl = [[UIRefreshControl alloc] init];
if (@available(iOS 10.0, *)) {
    self.tableView.refreshControl = refreshControl;
} else {
    [self.tableView addSubview:refreshControl];
}

Now I am presenting another viewController which has options to select filters. And after selecting those filters you come back again to current viewController having refreshControl.

I have added below code in viewDidAppear() for manually calling beginRefreshing:

if (self.filterChanged) {
    self.filterChanged = NO;
    [self.activityTableView setContentOffset:CGPointMake(0, - refreshControl.frame.size.height) animated:YES];
    [refreshControl setHidden:NO];
    [refreshControl beginRefreshing];
}

I have used setContentOffset for scrolling back to top and showing refreshControl. The only problem is suppose my tableView is half scrolled in between then there is a big gap between refreshControl.

If my tableView is not scrolled then it works fine like I have pulled down to refresh, but if it is half scrolled then inspite of giving setContentOffset there is a big gap between refreshControl and tableview.

1

1 Answers

0
votes

You need to wait for the scroll to finish before you can start the refresh. Unfortunately there is no completion block on setContentOffset, so try this.

After creating your refresh control set the target and create a corresponding method.

[_refreshControl addTarget:self action:@selector(refreshRequested) forControlEvents:UIControlEventValueChanged];

- (void)refreshRequested {
    [_activityTableView reloadData];
}

In your viewDidAppear you scroll to top and refresh when done.

[UIView animateWithDuration:0.25 delay:0 options:0 animations:^(void){        
    self.activityTableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
} completion:^(BOOL finished){
    [self.refreshControl beginRefreshing];
    [self refreshRequested];
}];

When finished loading you need to end refresh and make sure to scroll back to zero position.

- (void)finishedLoading {
    [_refreshControl endRefreshing];
    [self.activityTableView setContentOffset:CGPointMake(0, 0) animated:YES];
}