UIRefreshControl - Pull To Refresh in iOS 7 [duplicate]

I'm trying to get the pull to refresh feature on iOS 7 in my Table View. In my viewDidLoad, I have:

refreshControl = [[UIRefreshControl alloc] init];
[self.mytableView setContentOffset:CGPointMake(0, refreshControl.frame.size.height) animated:YES];
[refreshControl beginRefreshing];
[refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

I then run:

-(void)refreshTable {
    [self.mytableView reloadData];
    [refreshControl endRefreshing];
}

On iOS 6, this would mean that as you pull down on the table view, it would show the circular arrow that would get stretched out as you pull, and after pulled far enough, it would refresh. Right now, I see no circular arrow. What am I missing?


Solution 1:

You do not have to explicitly set frame or start UIRefreshControl. If it is a UITableView or UICollectionView, it should work like a charm by itself. You do need to stop it though.

Here is how you code should look like:

- (void)viewDidLoad {
    [super viewDidLoad];
    refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

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

In your refreshTable function, you need to stop it when you are done refreshing your data. Here is how it is going to look like:

- (void)refreshTable {
    //TODO: refresh your data
    [refreshControl endRefreshing];
    [self.mytableView reloadData];
}

Please note that if you are refreshing your data asynchronously then you need to move endRefreshing and reloadData calls to your completion handler.

Solution 2:

You forgot to attach the UIRefreshControl to your table view.

Change your viewDidLoad to

  refreshControl = [[UIRefreshControl alloc]init];
  [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
  [self setRefreshControl:refreshControl];

P.S. Your view controller should be a subclass of UITableViewController.