傲视苍穹iOS《Swift》VIP专题首页投稿(暂停使用,暂停投稿)iOS Developer

swift 3.0 tableview 添加上拉加载和下拉刷新

2017-06-13  本文已影响380人  旅途开发者

下拉刷新

tableview自带下拉刷新控件 UIRefreshControl ,添加到tableview上,添加事件即可

refreshAction = UIRefreshControl.init() //创建刷新控件

refreshAction.addTarget(self, action: #selector(refreshDown), for: .valueChanged) // 添加事件

refreshAction.attributedTitle = NSAttributedString.init(string: "正在下拉刷新")  // 修改文字内容

_carTableView.addSubview(refreshAction) //添加

//MARK- 下拉刷新

func refreshDown(){

pageNumber = 1

getDataHttps()

}

上拉加载

因为tableview本身没有上拉加载,我这里仅仅提供一个思路,有兴趣的可以参考一下

首先,创建一view,添加上点击事件

upView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: WindowWidth, height: 40))

upView.backgroundColor = UIColor.white

let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: WindowWidth, height: 40))

label.font = UIFont.systemFont(ofSize: 14)

label.textAlignment = .center

label.text = "点击加载更多"

label.isUserInteractionEnabled = true

let tap = UITapGestureRecognizer.init(target: self, action: #selector(refreshUp))

label.addGestureRecognizer(tap)

upView.addSubview(label)

在tableview的delegate中有个方法 

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)    在该方法中可以判断tableview滑动到位置,判断如果将要显示最后一个cell的时候,做如下处理

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

DeBugLog.printSwift("这里显示最后一个cell")

    if indexPath.row == _dataArray.count - 1 {

      _carTableView.tableFooterView = upView; //这里如果是最后一个cell,加载一个view在tableview的footview上

    }

}

view上的点击事件即为加载数据的处理,但是要记得把footview上的view删除,否则就会有重复的view加到tableview上

上一篇下一篇

猜你喜欢

热点阅读