46

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?

alexgophermix
  • 4,189
  • 5
  • 32
  • 59
Nivesh666
  • 487
  • 1
  • 4
  • 9

2 Answers2

177

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.

Itachi
  • 5,777
  • 2
  • 37
  • 69
Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53
  • Does it work with a Collection View? I was under the impression that refresh control works well only with a UITableView – rounak Feb 27 '14 at 09:05
  • @rounak - yes works like charm with `UICollectionView` too, I am currently using it in one of my projects. – Yas Tabasam Feb 27 '14 at 17:02
  • @YasKuraishi could you please tell me what is the difference between [self.tableView addSubview:refreshControl]; and [self setRefreshControl:refreshControl]; – Johnykutty Jun 16 '14 at 08:51
  • 6
    @Johnykutty 'refreshControl' is built-in synthesize property. You can set instantiated UIRefreshControl to it, and don't need to "addSubview" it. – dimpiax Jun 30 '14 at 17:27
  • Very simple to do ;) – ErasmoOliveira Jun 11 '15 at 12:22
  • 1
    UIRefreshControl should work with any UIScrollView derivated class; so UICollectionView would do as well as UITableView! – LaborEtArs Jan 13 '16 at 07:40
  • @YasT. Thankyou , it works. Is it possible to change spinning indicator – ios Sep 14 '17 at 05:34
8

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.

Ayush Goel
  • 3,134
  • 27
  • 36
  • 2
    False, your view controller does not need to be a subclass of `UITableViewController`, you just need a tableView as a property of your `UIViewController` and implement `` – aramusss Feb 24 '15 at 09:40
  • @aramusss `refreshControl` is a property on `UITableViewController` and not on `UITableView`. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewController_Class/index.html#//apple_ref/occ/instp/UITableViewController/refreshControl Can you please link to some documentation (or an example) where what you say is possible. – Ayush Goel Feb 25 '15 at 06:18
  • you are right, but it just works without UITableViewController as I said. You can find an example from other people here: http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller – aramusss Feb 25 '15 at 09:17
  • aramusss is correct. You do not need a UITableViewController. You just need a UITableView. I could not find documentation for that, but have done it many times. – datWooWoo Nov 24 '15 at 15:58
  • 2
    `refreshControl` property of `UITableView` is available only on iOS10. In iOS 9 it's only in `UITableViewController` – surfrider Feb 02 '17 at 09:50