[IOS] Use UIRefreshControl to implement UITableView pull-down refresh (Swift Version)

Source: Internet
Author: User

[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 .}*/}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.