Day7 - 下拉刷新,NSDate

先把明天的写了,明天休息 啊哈哈哈哈😁

效果图:

效果图如下

<br />

<br />

PS:纯代码搭建,并且只有一个TableView,布局就不解释了

<br />

1、因为代码简单的封装了下,所以先创建几个类先

如下:
PQRefreshVC: UITableViewController
继承tableViewController

PQRefresh: UIRefreshControl
继承UIRefreshControl

PQTBDataSource: NSObject , UITableViewDataSource
继承NSObject,包含UITableViewDataSource协议

OK,先看

  • 1 PQRefresh

import UIKit

class PQRefresh: UIRefreshControl {
    
    var lastUpdateTime : String = "第一次更新"
    //定义一个闭包
    var didRefreshBlock:(() -> ())?
    
    //重写初始化方法,并设置样式,添加事件
    override init() {
        super.init()
        backgroundColor = UIColor(red:0.113, green:0.113, blue:0.145, alpha:1)
        let attributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        attributedTitle = NSAttributedString(string: lastUpdateTime,attributes: attributes)
        tintColor = UIColor.redColor()
        addTarget(self, action: #selector(self.didRefresh), forControlEvents: .ValueChanged)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //定义一个私有方法
    @objc private func didRefresh(){
        if didRefreshBlock != nil {
            didRefreshBlock!()
        }
        lastUpdateTime = "最后一次更新时间: \(getNowTime())"
        let attributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        attributedTitle = NSAttributedString(string: lastUpdateTime,attributes: attributes)
        endRefreshing()
    }
    
    //获取当前时间
    func getNowTime() -> String{
        let nowTime = NSDate()
        let dataFormater = NSDateFormatter()
        dataFormater.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dataFormater.stringFromDate(nowTime)
    }
    
    //保存结束刷新代码,相当于保存block,在合适的时候调用
    func pqRefreshFinished(finish : () -> ()){
        didRefreshBlock = finish
    }

}

<br />

  • 2 在看 PQRefreshVC

添加两个属性:

    var pqRefreshControl:PQRefresh = PQRefresh()
    var pqUpdateData:((tableView : UITableView) -> ())?

<br />
在ViewDidLoad中添加

        self.refreshControl = pqRefreshControl
        
        pqRefreshControl.pqRefreshFinished {
            if self.pqUpdateData != nil{
                self.pqUpdateData!(tableView: self.tableView!)
            }
        }

<br />
再添加一个方法

func pqTableViewRefreshFinished ( block:(tableView : UITableView) -> ()) {
        pqUpdateData = block
    }
  • 3 PQTBDataSource

import UIKit
//定义了一个闭包,不过我更喜欢说成Block
typealias pq_dataSourceBlcok = (cell : AnyObject,item :AnyObject) -> ()

class PQTBDataSource: NSObject , UITableViewDataSource {
    //定义三个属性
    var cellIdentifier : String?
    var items = Array<AnyObject>()
    var feedBack :((cell : AnyObject,item :AnyObject) -> ())?
    //定义一个静态方法,创建对象并返回
    static func initWith(cellIdentifier : String,items : NSArray ,block : pq_dataSourceBlcok) -> PQTBDataSource{
        let source = PQTBDataSource()
        source.items = items as Array<AnyObject>
        source.cellIdentifier = cellIdentifier
        source.feedBack = block
        return source
    }
    
    //根据IndexPath查找对象
    func itemWithIndexPath(indexPath : NSIndexPath) -> AnyObject{
        return self.items[indexPath.row]
    }
    
    //根据数组返回有多少个cell
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    //把cell和item打包,使用闭包传出去
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : AnyObject = tableView.dequeueReusableCellWithIdentifier(cellIdentifier!, forIndexPath: indexPath)
        
        if feedBack != nil {
            feedBack!(cell :cell,item: itemWithIndexPath(indexPath))
        }
        
        return cell as! UITableViewCell
    }
    
    //提供一个借口,更新数组
    func updateDatas(items : NSArray){
        self.items = items as Array
    }
}

<br />

  • 4最后在ViewController中申明属性

//tableview
    var tableViewController = PQRefreshVC(style: .Plain)
    //刷新控件
    var refreshControl : PQRefresh = PQRefresh()
    
    //cell identifier
    let cellIdentifier = "myCellIdentifier"
    
    //data
    var data :Array = ["🤗🤗🤗🤗🤗", "😅😅😅😅😅", "😆😆😆😆😆"]
    let newFavoriteEmoji = ["我是新增的","🏃🏃🏃🏃🏃", "💩💩💩💩💩", "👸👸👸👸👸"]
    
    var pqDataSouce :PQTBDataSource?

在ViewDidLoad中添加如下代码

let myTableView = tableViewController.tableView
        myTableView.backgroundColor = UIColor(red:0.092, green:0.096, blue:0.116, alpha:1)
        
        pqDataSouce = PQTBDataSource.initWith(cellIdentifier, items: data, block: { (cell, item ) in
            
            let myCell = cell as! UITableViewCell
            let myItem = item as! String
            
            myCell.textLabel!.text = myItem
            myCell.textLabel!.textAlignment = NSTextAlignment.Center
            myCell.textLabel!.font = UIFont.systemFontOfSize(50)
            myCell.backgroundColor = UIColor.clearColor()
            myCell.selectionStyle = UITableViewCellSelectionStyle.None
        })
        
        myTableView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
        
        myTableView.dataSource = pqDataSouce
        myTableView.delegate = self
        myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
        
        myTableView.rowHeight = UITableViewAutomaticDimension
        myTableView.estimatedRowHeight = 60.0
        
        view.addSubview(myTableView)
        weak var weakSelf = self
        tableViewController.pqTableViewRefreshFinished { (tableView) in
            weakSelf!.data += weakSelf!.newFavoriteEmoji
            weakSelf!.pqDataSouce?.updateDatas(weakSelf!.data)
            myTableView.reloadData()
        }

如果你编译错误了:

请记得在ViewController中添加 UITableViewDelegate

还是不行:

Demo - TableViewRefreshControl

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,200评论 30 471
  • 转:http://www.cocoachina.com/programmer/20151019/13746.htm...
    Style_伟阅读 1,330评论 0 3
  • iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 开篇 iOS应用架构谈 view层的组织和调用方案...
    其实也没有阅读 1,832评论 0 15
  • 车刚刚使出北京。 隔壁男人轻声问,你是哪滴?小女孩说,哈尔滨的。奥,在北京上学?没,在北京上班。 你一句我一句,还...
    小西瓜667阅读 230评论 0 0
  • 文|宋玥 什么是网红?网络红人。 网络上最红的人?那么那些天天轰炸网络头版头条的明星不应当属最红的网红吗?当然有所...
    宋玥阅读 336评论 0 0