[IOS] Use UIRefreshControl to implement UITableView pull-down refresh (Swift Version)
First, initialize the relevant data in viewDidLoad:
Override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view. // Add refreshControl. addTarget (self, action: "refreshData", forControlEvents: UIControlEvents. valueChanged) refreshControl. attributedTitle = NSAttributedString (string: "") newsTableView. addSubview (refreshControl )}
In this way, you can see the pull-down Refresh:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD6908/large + PHA + PHByZSBjbGFzcz0 = "brush: java;"> // refresh data func refreshData () {let bquery = BmobQuery (className: "News") bquery. findObjectsInBackgroundWithBlock ({array, error in self. dataArray = array self. newsTableView. reloadData () self. refreshControl. endRefreshing ()})}
The complete code is as follows. Because bmob api calls are involved, some code may not be readable, but it does not affect the use of basic functions.
/// NewsViewController. swift // WomenWorkerGuide /// Created by why on 9/20/14. // copy right (c) 2014 why. all rights reserved. // import UIKit/*** news */class NewsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {@ IBOutlet weak var newsTableView: UITableView! Var refreshControl = UIRefreshControl () var dataArray: [AnyObject] = [AnyObject] () override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view. self. automaticallyAdjustsScrollViewInsets = false // Add refreshControl. addTarget (self, action: "refreshData", forControlEvents: UIControlEvents. valueChanged) refreshControl. attributedTitle = NSAttributedString (string: "Automatically Refresh after release") newsTableView. addSubview (refreshControl) refreshData ()} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} // refresh the data func refreshData () {let bquery = BmobQuery (className: "News") bquery. findObjectsInBackgroundWithBlock ({array, error in self. dataArray = array self. newsTableView. reloadData () self. refreshControl. EndRefreshing ()} // MARK:-UITableViewDataSource func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {return dataArray. count;} func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {let cell = UITableViewCell (style: UITableViewCellStyle. subtitle, reuseIdentifier: "newsCell") let obj: BmobObject = dataArray [inde XPath. row] as BmobObject cell. textLabel ?. Text = obj. objectForKey ("title")? String let dateFormatter = NSDateFormatter () dateFormatter. dateFormat = "MM dd, yyyy" let str = dateFormatter. stringFromDate (obj. createdAt) cell. detailTextLabel ?. Text = str return cell;}/* // MARK:-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue (segue: UIStoryboardSegue !, Sender: AnyObject !) {// Get the new view controller using seue. destinationViewController. // Pass the selected object to the new view controller .}*/}