While the title might be evocative of a cheap adult film, the pull-down to refresh mechanism is now the de-facto control to retrieve new data from a server. All social apps including the official Twitter and Facebook for iOS implement this functionality currently. So how would you implement this rather neat feature? Constructing your own custom Pull Down to Refresh mechanism might be a little painful and time-consuming but luckily there are two well-coded open source frameworks that you can easily use to integrate this feature into your project.
The first of the two frameworks is by Leah Culver. You can find the source here. I personally preferred using the framework provided by Enermego as it is easy to integrate and looks better than any other open-source available out there. The source code can be found here. I will give you a brief introduction on how you can use this framework in your own project.
Download the source code and copy the folder titled ‘EGOTableViewPullRefresh’ into your Xcode project. The folder essentially consists of a .h and a .m file along with few images to indicate data loading.
Now to get your hands dirty with the implementation.
First create an instance of the Class that you have just imported into your project along with a BOOL variable to indicate whether the table is still reloading. Also, mention the delegate that you will be using. Here’s how it will look:
1 2 3 4 5 6 7 8 |
<blockquote> #import "EGORefreshTableHeaderView.h" @interface MyViewController : UIViewController { UITableView *myTableView; EGORefreshTableHeaderView *_refreshHeaderView; BOOL _reloading; }</blockquote> |
Now in your viewDidLoad method of your .m file, initialize your myTableView and add it as a subView of your main view. Alternately you can create your tableView in Interface Builder as i chose to do.
Add the following code snippet to your viewDidLoad method:
1 2 3 4 5 6 7 8 9 |
<blockquote> if (_refreshHeaderView == nil) { EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.myTableView.bounds.size.height, self.view.frame.size.width, self.myTableView.bounds.size.height)]; view.delegate = self; [self.myTableView addSubview:view]; _refreshHeaderView = view; } [_refreshHeaderView refreshLastUpdatedDate];</blockquote> |
The above lines of code essentially create the ‘refresh-enabled’ table with the same dimensions of myTableView and assigns self as the delegate. We also call the method that refreshes the last updated date. Now we add the delegate methods of the EGORefreshTableHeaderView and the UIScrollView classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<blockquote> #pragma mark - #pragma mark Data Source Loading / Reloading Methods - (void)reloadTableViewDataSource{ // should be calling your tableviews data source model to reload [myService myCustomMethodToGetData]; _reloading = YES; } - (void)doneLoadingTableViewData{ // model should call this when its done loading _reloading = NO; [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.requestsTableView]; } #pragma mark - #pragma mark UIScrollViewDelegate Methods - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView]; } #pragma mark - #pragma mark EGORefreshTableHeaderDelegate Methods - (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{ [self reloadTableViewDataSource]; [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:1.0]; } - (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{ return _reloading; // should return if data source model is reloading } - (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{ return [NSDate date]; // should return date data source was last changed }</blockquote> |
That’s all there is to it. You can tinker with your delay values depending on your project. But this nifty little framework will give you a readymade Pull Down to Reload feature that you can have up and running in no time. Do keep in mind that this code is about a year old and was not written with Xcode 4.2 and its ARC feature in mind. Hence you will get compiler errors when you encounter [myVariable release] in the code. Just comment or delete those lines of code and it will run just fine.
PS: In case you do run into any trouble, there is a demo project that comes bundled with the gitHub download that may help you out.
1 comment
Fsit Fsit says:
Mar 26, 2013
Thanks so much… It is easy to implement and work preety cool.