码迷,mamicode.com
首页 > 移动开发 > 详细

UIRefreshControl --- IOS中用于刷新UITableView等的控件

时间:2015-05-09 19:06:17      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:refresh

IOS开发中, 经常需要添加UITableView的下拉刷新功能, 使用UIRefreshControl就可以非常方便得实现.

UIRefreshControl

下边是UIRefreshControl的头文件.

import Foundation
import 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等的控件

标签:refresh

原文地址:http://blog.csdn.net/icetime17/article/details/45603125

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!