先把明天的写了,明天休息 啊哈哈哈哈😁
效果图:
<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
还是不行: