samvermette.com

APR 24, 2012

SVPullToRefresh

What if adding pull to refresh to a UITableView was easy as doing:

[tableView addPullToRefreshWithActionHandler:^{
    // reload data
}];

It now is with SVPullToRefresh.

SVPullToRefresh is designed with 2 key principles in mind:

Extending UIScrollView by adding a method that would do this seemed like the perfect way to go, except Objective-C categories normally don’t allow synthesized properties. So how would I allow stopping the loading animation, let alone customizing the view? Thanks to @seb_morel ’s “Demystifying the Objective-C runtime” talk from last week, I used the Objective-C runtime to add a pullToRefreshView dynamic property to UIScrollView.

Stopping the loading animation can therefore be done using:

[tableView.pullToRefreshView stopAnimating]

You can also customize some of the view’s attributes:

tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
tableView.pullToRefreshView.textColor = [UIColor whiteColor];
tableView.pullToRefreshView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;

Unlike other implementations, SVPullToRefresh doesn’t force you to use a UIViewController subclass, initialize views, or implement a bunch of delegate methods. It gets all that stuff out of the way and lets you spend time on what matters; the code that will run on refresh.


hello@samvermette.com