Using UIRefreshControl

The UIRefreshControl provides a nice refresh control for a UITableViewController. It's fairly easy to setup and use programmatically.

To get started we will initialize the control in the loadView method. It will look something like:

-(void)loadView
{
    self.refreshControl = [[UIRefreshControl alloc]init];
    [self.refreshControl addTarget:self action:@selector(tableViewWillRefresh) forControlEvents:UIControlEventValueChanged];
}

Additionally, you will need to create an action target for the UIControlEventValueChanged event. The method this target calls is where we place our refresh code. In the code above, we pointed to a method called tableViewWillRefresh.

- (void)tableViewWillRefresh
{
    NSLog(@"Refreshing table");

    // this is my function that loads data for my table
    [self loadDataForTable];
}

Finally, when the data load is complete, you can call the endRefreshing method on the refresh control to stop the spinner. This can be placed in the callback.

- (void)dataForTableDidLoad 
{
    [self.refreshControler endRefreshing];

      // do some other stuff to ensure your table data refreshes
}

Additionally, you can programmatically trigger the refresh display by using the beginRefreshing method. That's all there is to this thing. Pretty easy.

comments powered by Disqus