This is a UIView-based pull-to-refresh solution based on EGOTableViewPullRefresh, which can be found here. PullToRefreshViewController uses a subclassed UIViewController and a generic UITableView to create pull-to-refresh functionality.
-
Clone the project:
git clone git://github.com/jasonhawkins/PullToRefreshViewController.git -
Drag the PullToRefreshViewController folder into your open view-based Xcode project.
-
Ensure that "Copy items into destination group's folder" is checked.
-
Pull-to-refresh requires the QuartzCore framework.
4.1. Select the project name from the Navigator panel on the left.
4.2. Select Build Phases from the navigation across the top.
4.3. Press the disclosure triangle next to Link Binary With Libraries.
4.4. Press the plus button and search for QuartzCore.framework.
4.5. Press the Add button to add the framework.
-
Subclass PullToRefreshViewController in your UIViewController.
5.1. Select the UIViewController you want to use as your subclass from the Navigator panel on the left.
5.2. Add
#import "PullToRefreshViewController.h"to the top of the UIViewController's .h that you wish to subclass.5.3. Change
@interface SomeViewController : UIViewControllerto@interface SomeViewController : PullToRefreshViewController. -
This subclass assumes you already have a UITableView ready to use. If not, switch to your view's .xib file and drag in a new UITableView from the Utilities panel on the right (this panel may be hidden by default). The UITableView can be any height you want. It doesn't have to fill the view.
-
Connect your UITableView to it's delegate and datasource by control-clicking from the UITableView to File's Owner.
-
Connect your File's Owner tableView outlet to your UITableView by control-clicking from File's Owner to your UITableView and selecting "tableView" from the popup.
This subclass requires you to override a few methods for it to work properly. These include delegate methods for both the refreshing behavior and the standard table view. You don't have to worry about conforming to any protocols however, since that's done for you in PullToRefreshViewController.h.
Below are the methods you will need to override. Copy and pasting them into your subclassed .m file as-is will give you a fully functional demo to start with. If you'd prefer just the method stubs, they are as follows:
- (void)reloadTableViewDataSource;- (void)dataSourceDidFinishLoadingNewData:(NSNumber *)loadedData;- (void)dataSourceDidFailPresentingError;- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)reloadTableViewDataSource
{
// Should be calling your tableview's model to reload.
[super performSelector:@selector(dataSourceDidFinishLoadingNewData) withObject:nil afterDelay:3.0];
}
- (void)dataSourceDidFinishLoadingNewData:(NSNumber *)loadedData
{
// Should check if data reload was successful.
if ([loadedData boolValue]) {
[refreshHeaderView setCurrentDate];
[super dataSourceDidFinishLoadingNewData:nil];
[self.tableView reloadData];
} else {
[super dataSourceDidFinishLoadingNewData:nil];
// Present an informative UIAlertView
[self dataSourceDidFailPresentingError];
}
}
- (void)dataSourceDidFailPresentingError
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Network Error"
message:@"Unable to contact the server.\n Please try again later."
delegate:self
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
return cell;
}
- You might see build warnings that begin with
Warning: Multiple build commands for output file…To fix this look for the .git group under the PullToRefreshViewController group in the Navigator panel in Xcode. Remove the .git group. If the warnings persist, try looking under Copy Bundle Resources for duplicate files.