UIRefreshControl --- IOS中用於重新整理UITableView等的控制項

來源:互聯網
上載者:User

標籤: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等的控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.