iOS --- 使用UIRefreshControl来刷新UITableView等控件

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()
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值