Adding a Pull to Refresh idiom to your UITableViewController

This is just a quick and easy post for something that I thought would be a pain, but is quick and easy to add: A UIRefreshController.

UIRefreshControllers are what you use to do a Pull-to-refresh on any subclass of UITableViewController. Typically, this allows your users to see if there is any new data available. Apple recommends this only for UITableViewControllers. Using it elsewhere is possible, but can give unpredictable results.

How easy is it? This easy:

1. Add a UIRefreshControl property to your @interface()

2. Add a method to set up the refresh control. Call it from ViewDidLoad (because it only needs to be set up once). The refresh will now start any time a user pulls down on the screen. If using UIControl actions like UIControlEventValueChanged is a mystery to you, check out the UIControl Class Reference for what these control actions are all about.

3. Handling a UIRefresh:

This can be anything you need, but typically you would be updating your data model. This can be tricky as you'll have to decide how many more objects to load and so on, but that's all up to how you run your queries.

4. Turning off refresh:

The refresh doesn't shut itself off, so remember to call [self.refresher endRefreshing]; when you are about to update your table view.

Easy peasy!

And for those developers who love da BLING!

Now you might want to mess with the colors and so on. As usual, turn to the apple documentation for the UIRefreshControl Class. Although there's not much to see. All you can trick out is the color and title text. F'rinstance this...:

...gives you this eyesore:

Although you CAN do this, with UI, less is more, so I recommend just using the basic default refresh spinner. But it's always fun to mess around :)

Search By Tags