Pull down to refresh table view

An easy way to add twitter like pull down to refresh function to any table view. No dependency to other libraries, only include the 4 source files and 1 resource file to use it.

How does it work?
A UIPullToReloadHeaderView is added into the top of the UITableView as a subview. UITableView is actually a UIScrollView so we override the UIScrollDelegate scrollViewWillBeginDragging:, scrollViewDidScroll: and scrollViewDidEndDragging: to track the scroll position when the UITableView is dragging, and perform animation according to the scroll position. When loading is in progress, in order to show the UIPullToReloadHeaderView, we inset the UIScrollView so that the top extra part will be shown.

How to use?
In your original table view header file:

@interface RootViewController : UITableViewController {

Subclass it to UIPullToReloadTableViewController:

@interface RootViewController : UIPullToReloadTableViewController {

When pull to refresh is triggered, it will call pullDownToReloadAction, so we added:

-(void) pullDownToReloadAction {
}

You should add your own code for reloading the data. In our code, I just wait 3 seconds and perform another selector.

When reload is finished, you should update the interface to stop the activity indicator:

[self.pullToReloadHeaderView finishReloading:self.tableView animated:YES];

In the sample code, the app will add one more row at the top of the table after 3 seconds the pull down to refresh action is triggered.

If you code is not derived from UITableViewController, you can also check UIPullToReloadTableViewController on how to embed the UIPullToReloadHeaderView into your view.

The source code is attached and distributed in apache license.

附加檔案大小
pullToReloadTableViewTest.zip60.33 KB

Comments :

PullToReload if you have a search bar control on table header

Hi,
I am glad to run into this great tutorial on table refreshes. Thank you for posting.
I just had a question that I have not been able to figure out.
I have a TableView that also performs searches. When I pull down the table to reload, the arrow does not turn toward up and no method is called. If I don't put the search bar everything works great.
Any Help greatly appreciated.
Thanks.

The search bar will also put

The search bar will also put to the negative inset of the table, so you have offset the pull to reload view a little bit and mod the code to fit the search bar.