Pull to refresh UITableView | UIRefreshViewControl

I use UITableViews in almost every app I build, and sometimes I need to implement a pull to refresh like a boss. Its pretty easy so lets get started.

Adding a UIRefreshViewControl

Lets set up the scenario first. You have a storyboard with a view controller, and inside the view controller is the the UITableView that you want to add the UIRefreshViewControl to. You cannot add a UIRefreshViewControl in the storyboard so we will have to add it to our table view in the ViewController.

What if you don’t have a UITableViewController!

You can’t add a UiRefresViewControl directly to a UITableView so if you are inside a UIViewControlelr and only have tableview, you can just add one!

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableViewController *tableViewController = [[UITableViewController alloc] init];
    tableViewController.tableView = tweetsTableView;

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refreshTweets) forControlEvents:UIControlEventValueChanged];
    tableViewController.refreshControl = self.refreshControl;
    self.refreshControl.tintColor = [UIColor colorWithRed:(254/255.0) green:(153/255.0) blue:(0/255.0) alpha:1];

}

First we create the UITableViewController and set the UITableView from the storyboard as its tableview property. Next I alloc and init the refreshControl and add a target and action. This is the callback method when the tableview is pulled to refresh!

Hide the UIRefreshControl

- (void)refreshTweets{
    // refresh logic 
    //End refresh control
    [self.refreshControl endRefreshing];
}

The endRefreshing method removes the loading icon from the top of the TableView and we are back where we started with our TableView!

Author: barrettbreshears

I don't take showers, only blood baths.