[iOS] 使用UIRefreshControl 實現 UITableView下拉重新整理(Swift版本)

來源:互聯網
上載者:User

標籤:swift   ios   

首先,在viewDidLoad中初始化相關資料:


  override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view.                //添加重新整理        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)        refreshControl.attributedTitle = NSAttributedString(string: "鬆手重新整理新聞")        newsTableView.addSubview(refreshControl)            }

這樣下拉重新整理已經可以看到了:




接下來把Selector 完善一下,添加refreshData方法:

 // 重新整理資料    func refreshData() {        let bquery = BmobQuery(className: "News")        bquery.findObjectsInBackgroundWithBlock({array, error in            self.dataArray = array            self.newsTableView.reloadData()            self.refreshControl.endRefreshing()        })    }


完整的代碼如下,因為涉及到 BMOB API 的調用,所以有些代碼可能看不懂,但是並不影響準系統的使用。

////  NewsViewController.swift//  WomenWorkerGuide////  Created by why on 9/20/14.//  Copyright (c) 2014 why. All rights reserved.//import UIKit/***  新聞*/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                //添加重新整理        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)        refreshControl.attributedTitle = NSAttributedString(string: "鬆開後自動重新整理")        newsTableView.addSubview(refreshControl)        refreshData()    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        // 重新整理資料    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[indexPath.row] as BmobObject           cell.textLabel?.text = obj.objectForKey("title") as? String                       let dateFormatter = NSDateFormatter()        dateFormatter.dateFormat = "yyyy年 MM月 dd日"        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 segue.destinationViewController.        // Pass the selected object to the new view controller.    }    */}



[iOS] 使用UIRefreshControl 實現 UITableView下拉重新整理(Swift版本)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.