Basics of Pull to Refresh for Swift Developers

Updated on September 21, 2016 – Swift 3.0

Implementing “pull to refresh” is a common need that arises when working with table views. There are typically two scenarios that folks find themselves in when attempting to implement this feature:

  1. They’re working with a UITableViewController
  2. They’re working with a non-UITableViewController, but their view incorporates a regular UITableView, either taking up the whole screen, or a smaller portion of it

This entry will explore both scenarios to help you get up and running quickly with implementing pull to refresh for your Swift iOS app.

Example scenario

For this guide, suppose that we have a list of movies that we’d like to display in a table view. Pulling to refresh will fetch more movies and update the table view to show the new ones.

If you’re the type that likes to simply dive into a working example, both implementations are available to download from GitHub:

Example projects

Note: Code in the main article below is written in Swift 3.0, but code examples for Swift 2.3 are found in the example projects above.

Movies are represented by a simple struct:

The table view (regardless of whether we use a UITableViewController or a regular UIViewController) has the following setup…

Initial data source values:

Setting up the table view’s data source protocol methods depends on whether you’re using a UITableViewController or a regular UIViewController with a table view as one of its content views, so we’ll cover those in the individual examples.

Implementing with UITableViewController

Finish example setup

When working with a UITableViewController, we simply override the data source method implementations. The following is how I’ve chosen to do it for this example:

Enable refreshing in Storyboard

When you’re working with a UITableViewController, the solution is fairly simple: First, Select the table view controller in your storyboard, open the attributes inspector, and enable refreshing:

Table View Controller - Enable Refreshing

A UITableViewController comes outfitted with a reference to a UIRefreshControl out of the box. You simply need to wire up a few things to initiate and complete the refresh when the user pulls down.

Override viewDidLoad()

In your override of viewDidLoad(), add a target to handle the refresh as follows:

Here are a couple of things to observe about the code above:

  1. Swift’s new #selector feature helps with specifying which action will handle the refresh. Since I’ve specified ViewController.handleRefresh(_:) (note the underscore and the colon!) as the action argument, I need to define a function in this UITableViewController class with the same name. Additionally, the function should take one argument.
  2. We’d like this action to be called for the UIControlEvent called ValueChanged.

Implement handleRefresh function

The handleRefresh: function may look something like the following:

That should complete the pull to refresh implementation when you’re working with a UITableViewController!

Implementing with regular view controller + UITableView

Finish example setup

When working with a regular UIViewController, there are a few extra steps involved in getting things set up:

  1. Create an IBOutlet from the storyboard to the view controller
  2. Wire up the table view’s data source and delegate from the storyboard
  3. Implement the required table view data source methods

Creating the IBOutlet is a matter of control+clicking and dragging from the table view in the Storyboard to the view controller code to create the outlet.

To wire up the table view’s data source and delegate in the Storyboard, control+click the table view and drag up to the yellow view controller icon:

Wire up table view's data source and delegate

The data source protocol method implementations may look something like this:

Set up UIRefreshControl

Whereas a UITableViewController comes pre-fit with a UIRefreshControl, a regular UIViewController does not. It’s simple enough to set one up though. Here is a snippet defining a lazily instantiated variable which creates and configures a UIRefreshControl:

The most complicated thing about the code I just proposed is how the UIRefreshControl instance is assigned lazily by means of the closure expression denoted by = { // ...closure body with setup code... }() in the above snippet. Using this approach allows me to complete the setup all in one spot without the use of optionals. You may prefer doing this another way. The bottom line goal is to have a UIRefreshControl instance that we can add to the table view (coming up).

As for the body of the closure expression, we’re adding a target-action to the UIRefreshControl instance, just like we did when we were dealing with a UITableViewController.

As with the UITableViewController example, note:
1. Since I’ve specified “handleRefresh:” (note the colon!) as the action argument, I need to define a function in this UITableViewController class with the same name. Additionally, the function should take one argument.
2. We’d like this action to be called for the UIControlEvent called ValueChanged.

Override viewDidLoad

Assuming that there is an outlet to the table view in the Storyboard, the next step is to add the UIRefreshControl as a subview to the table view:

Implement handleRefresh function

The handleRefresh function is implemented exactly as it was when we were dealing with a UITableViewController:

Wrapping up

Implementing “pull to refresh” is a common need that arises when working with table views. Here we’ve explored how to implement this feature using both a UITableViewController and with a regular view controller and a table view.