Reply to comment

strict warning: Only variables should be passed by reference in /var/www/www.waterworld.com.hk/sites/all/modules/captcha/captcha.inc on line 61.

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.

AttachmentSize
pullToReloadTableViewTest.zip60.33 KB

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.