標籤:refresh
IOS開發中, 經常需要添加UITableView的下拉重新整理功能, 使用UIRefreshControl就可以非常方便得實現.
UIRefreshControl
下邊是UIRefreshControl的標頭檔.
import Foundationimport UIKit//// UIRefreshControl.h// UIKit//// Copyright 2012-2014 Apple Inc. All rights reserved.//@availability(iOS, introduced=6.0)class UIRefreshControl : UIControl { /* The designated initializer * This initializes a UIRefreshControl with a default height and width. * Once assigned to a UITableViewController, the frame of the control is managed automatically. * When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event. */ init() var refreshing: Bool { get } var tintColor: UIColor! var attributedTitle: NSAttributedString? // May be used to indicate to the refreshControl that an external event has initiated the refresh action @availability(iOS, introduced=6.0) func beginRefreshing() // Must be explicitly called when the refreshing has completed @availability(iOS, introduced=6.0) func endRefreshing()}
可見, 其提供的調用方法很簡單, init()是建立該控制項必須的, 接下來就是 beginRefreshing() 和endRefreshing()這兩個控制重新整理的方法了.
使用執行個體
以UITableView的重新整理為例, UIRefreshControl的使用分為以下幾個步驟:
1. 初始化一個UIRefreshControl對象.
2. 添加重新整理時調用的方法, 這裡是refreshDataSource()方法.
3. 將UIRefreshControl對象添加到tableView中.
然後, 下拉裝置螢幕即可看到效果.
var refreshCtl = UIRefreshControl() var httpCtl: HttpController = HttpController() // outlets @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. httpCtl.delegate = self refreshCtl.attributedTitle = NSAttributedString(string: "下拉重新整理") refreshCtl.addTarget(self, action: "refreshDataSource", forControlEvents: UIControlEvents.ValueChanged) tableView.addSubview(refreshCtl) refreshDataSource() } func refreshDataSource() { refreshCtl.beginRefreshing() httpCtl.getDataViaNSURLConnection("http://0.0.0.0/dataapi") refreshCtl.endRefreshing() }
UIRefreshControl --- IOS中用於重新整理UITableView等的控制項