UIRefreshControl, code4app
During IOS development, you often need to add the pull-down refresh function of UITableView, which can be easily implemented by using UIRefreshControl.
UIRefreshControl
Below is the header file of 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()}
It can be seen that the call method provided by it is very simple. init () is required to create the control, followed by the inrefreshing () and endRefreshing () methods to control the refresh.
Use instance
Taking refresh of UITableView as an example, the use of UIRefreshControl involves the following steps:
1. initialize a UIRefreshControl object.
2. Add the method called when refreshing. Here is the refreshDataSource () method.
3. Add the UIRefreshControl object to tableView.
Then, drop down the device screen to see the effect.
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: "pull-down refresh") 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 ()}