Important note if your project doesn’t use ARC: you must add the -fobjc-arc
compiler flag to SVPullToRefresh.m
in Target Settings > Build Phases > Compile Sources.
SVPullToRefresh allows you to easily add pull-to-refresh and/or infinite scrolling functionalities to any UITableView
subclass with only 1 line of code. Instead of depending on delegates and/or subclassing UIViewController
, SVPullToRefresh extends UIScrollView
with the following 2 methods:
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler; - (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
- Drag the
SVPullToRefresh/SVPullToRefresh
folder into your project. - Add the QuartzCore framework to your project.
#import "SVPullToRefresh.h"
(see sample Xcode project in /Demo
)
[tableView addPullToRefreshWithActionHandler:^{ // refresh data // call [tableView.pullToRefreshView stopAnimating] when done }];
If you’d like to programmatically trigger the refresh (for instance in viewDidLoad), you can do so with:
[tableView.pullToRefreshView triggerRefresh];
You can temporarily hide/disable pull to refresh by setting the showsPullToRefresh
property:
tableView.showsPullToRefresh = NO;
The pullToRefreshView
view can be customized using the following properties:
@property (nonatomic, strong) UIColor *arrowColor; @property (nonatomic, strong) UIColor *textColor; @property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
You can access these properties through your scroll view’s pullToRefreshView
property.
For instance, you would set the arrowColor
property using:
tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
If you think it’s in the user’s interest to display the date of the last refresh, you can set the lastUpdatedDate
property of pullToRefreshView
:
tableView.pullToRefreshView.lastUpdatedDate = nil; // will display "Last Updated: Never" tableView.pullToRefreshView.lastUpdatedDate = [NSDate date]; // will display "Last Updated: 4/30/12 11:53 AM"
You can also configure how the date is displayed by setting the dateFormatter
property:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateStyle = NSDateFormatterLongStyle; dateFormatter.timeStyle = NSDateFormatterNoStyle; tableView.pullToRefreshView.dateFormatter = dateFormatter;
[tableView addInfiniteScrollingWithActionHandler:^{ // add data to data source, insert new cells into table view }];
You can temporarily hide/disable infinite scrolling by setting the showsInfiniteScrolling
property:
tableView.showsInfiniteScrolling = NO;
The infiniteScrollingView
(a UIView
subclass) can be customized using the following properties:
@property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
You can access these properties through your scroll view’s infiniteScrollingView
property.
SVPullToRefresh extends UIScrollView
by adding new public methods as well as a dynamic properties (thanks @seb_morel!). It uses key-value observing to track the scrollView’s contentOffset
, which removes the need for the view to be linked to the UIScrollViewDelegate
protocol.
SVPullToRefresh is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you’re using SVPullToRefresh in your project, attribution would be nice.
Big thanks to @seb_morel for his Demistifying the Objective-C runtime talk, which permitted the level of abstraction found in SVPullToRefresh.
Hat tip to Loren Brichter for inventing such a great UI mechanism.