【精】表格(UITableView)总结(3):刷新(UIRefreshControl)

来源:互联网 发布:vb与三菱plc通讯案例 编辑:程序博客网 时间:2024/04/28 07:48

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/45479815

1、前言

iOS 6 以后苹果官方引入了 UIRefreshControl,用法相对也比较简单。

2、演示代码

////  ViewController.swift//  UITableViewSample-UIRefreshControl////  Created by jinnchang on 15/5/21.//  Copyright (c) 2015年 Jinn Chang. All rights reserved.//import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {        var tableView:      UITableView!    var refreshControl: UIRefreshControl!        var data:           NSMutableArray!    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                self.title = "下拉刷新"                data = []                refreshControl = UIRefreshControl()        refreshControl.tintColor = UIColor.blackColor()        refreshControl.backgroundColor = UIColor.whiteColor()        refreshControl.attributedTitle = NSAttributedString(string: "下拉开始刷新")        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)                tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)        tableView.delegate = self        tableView.dataSource = self                tableView.addSubview(refreshControl)        self.view.addSubview(tableView)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    // 设置每个分段对应的行数    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return data.count    }        // 设置每行的具体内容    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell        if(cell == nil) {            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")        }        cell!.textLabel?.text = data.objectAtIndex(indexPath.row) as? String        return cell!    }        /// 下拉触发动作    func refreshAction() {        if(refreshControl.refreshing){            refreshControl.attributedTitle = NSAttributedString(string: "正在刷新")            refreshData()        }    }        /// 刷新数据    func refreshData() {        let dateFormatter = NSDateFormatter()        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"        let time = dateFormatter.stringFromDate(NSDate())                data.addObject(time)        refreshControl.attributedTitle = NSAttributedString(string: "最近一次更新时间为:\(time)")        refreshControl.endRefreshing()        tableView.reloadData()    }    }
Github上项目地址:https://github.com/jinnchang/SwiftSamples/blob/master/UITableViewSample-UIRefreshControl

4、结语

文章最后更新时间:2015年5月23日15:47:19


0 0
原创粉丝点击