Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
Nivesh666 Avatar asked Feb 27 '14 05:02

Nivesh666


2 Answers

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.

like image 55
Yas Tabasam Avatar answered Nov 07 '22 03:11

Yas Tabasam


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.

like image 29
Ayush Goel Avatar answered Nov 07 '22 04:11

Ayush Goel