回覆回應

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

回應

這個欄位的內容會保密,不會公開顯示。
  • 自動將網址與電子郵件位址轉變為連結。
  • 可使用的 HTML 標籤:<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 自動斷行和分段。
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

更多關於格式選項的資訊

CAPTCHA
這條問題是測試你是否真正的人類使用者。
Image CAPTCHA
Enter the characters shown in the image.