Xcode9.1:swift自定义tableview下拉刷新
来源:互联网 发布:淘宝众筹的产品怎么样 编辑:程序博客网 时间:2024/06/03 12:26
///自定义的tableview组件
//// MyTableView.swift// swjmanager//// Created by Jo on 2017/11/22.// Copyright © 2017年 swj. All rights reserved.//import UIKit@objc protocol MyTableViewDelegate: NSObjectProtocol { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat //刷新的时候触发 func tableViewRefreshing() @objc optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)}extension MyTableView: UITableViewDelegate { func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { if scrollView.contentOffset.y <= -MyTableView_PullView_Size.height && myRefreshControl.isRefreshing == true && pullView_Logo.isAnimating == false { showLoadingView() } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if let myDelegate = myDelegate { return myDelegate.tableView(tableView, heightForRowAt:indexPath) } return 60 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if let myDelegate = myDelegate { return myDelegate.tableView(tableView, heightForHeaderInSection: section) } return 0 } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { if let myDelegate = myDelegate { return myDelegate.tableView(tableView, viewForFooterInSection: section) } return nil } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if let myDelegate = myDelegate { return myDelegate.tableView(tableView, viewForHeaderInSection: section) } return nil } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { if let myDelegate = myDelegate { return myDelegate.tableView(tableView, heightForFooterInSection: section) } return 0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let myDelegate = myDelegate { myDelegate.tableView!(tableView, didSelectRowAt: indexPath) } }}class MyTableView: UITableView { let myRefreshControl = UIRefreshControl.init() let pullView_Content = UIImageView.init() //刷新时候的图案控件 let pullView_Logo = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: MyTableView_PullView_Size.height, height: MyTableView_PullView_Size.height)) weak var myDelegate: MyTableViewDelegate? //存放要刷新过程时候显示的动画的图片 lazy var loadingImages: [UIImage] = { var array: [UIImage] = [] for i in 1 ... 10 { let image = UIImage.init(named: "xiala_anima_progress_\(i)") array.append(image!) } return array }() override func awakeFromNib() { super.awakeFromNib() initRefreshControl() addRefreshViewForPull() initTB() } func initRefreshControl() { myRefreshControl.backgroundColor = UIColor.clear myRefreshControl.tintColor = UIColor.clear addSubview(myRefreshControl) } func initTB() { separatorStyle = .none delegate = self } //下拉时候的显示图案 func addRefreshViewForPull() { pullView_Content.frame = CGRect.init(x: 0, y: -40, width: bounds.size.width, height: MyTableView_PullView_Size.height) pullView_Content.backgroundColor = MyTableView_RefreshView_PullView_BGColor addSubview(pullView_Content) pullView_Logo.image = MyTableView_RefreshView_Pull_Image pullView_Logo.center = CGPoint.init(x: pullView_Content.center.x, y: 20) pullView_Content.addSubview(pullView_Logo) } fileprivate func showLoadingView() { if !pullView_Logo.isAnimating { pullView_Logo.animationImages = loadingImages pullView_Logo.animationRepeatCount = 0 pullView_Logo.animationDuration = 1 pullView_Logo.startAnimating() if let myDelegate = myDelegate { myDelegate.tableViewRefreshing() } } } //结束刷新 func endRefreshing() { myRefreshControl.endRefreshing() pullView_Logo.stopAnimating() }}
在控制器中使用:
//// OnlineGameViewController.swift// swjmanager//// Created by Jo on 2017/11/22.// Copyright © 2017年 swj. All rights reserved.//import UIKitextension OnlineGameViewController: UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 40 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: OnlineGameVC_TB_Identifier) if cell == nil { cell = UITableViewCell.init(style: .default, reuseIdentifier: OnlineGameVC_TB_Identifier) } cell?.textLabel?.text = "\(indexPath.row)" return cell! } }extension OnlineGameViewController: MyTableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 0 } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { return nil } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return nil } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { printLog(message: "点击了") self.tableView_main.endRefreshing() } func tableViewRefreshing() { printLog(message: "开始刷新") }}class OnlineGameViewController: BaseViewController { @IBOutlet weak var tableView_main: MyTableView! override func viewDidLoad() { super.viewDidLoad() //消除tb下移64px的bug navigationController?.navigationBar.isTranslucent = false initTB() } func initTB() { tableView_main.myDelegate = self tableView_main.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
ps:在sb或者xib拖出一个tableview控件后,需要设置它的class为我们自定义组件的名字。
阅读全文
0 0
- Xcode9.1:swift自定义tableview下拉刷新
- tableview下拉刷新
- TableView下拉刷新
- tableview下拉刷新
- 下拉刷新tableView
- tableview实现下拉刷新
- TableView下拉刷新崩溃
- 自定义tableview实现:下拉刷新,上拉加载
- swift详解之二十八---------自定义下拉刷新
- EGORefreshTable 实现TableView下拉刷新
- 下拉刷新(TableView中才可使用)
- IOS控件TableView下拉刷新
- tableView 下拉刷新动画demo
- swift UIRefreshControl() 下拉刷新
- IOS swift 自定义Tableview cell
- iOS swift webview下拉刷新
- 自定义ListView,下拉刷新
- Android自定义下拉刷新
- Scala 可变参数函数的灵活调用
- unity实现简单的加密使用次数限制时间限制功能
- CADMeister.v6.1-ISO 1DVD中文版,冲模设计软件
- oracle输出文本格式化命令
- Java多线程系列--【基础篇02】- 常用的实现多线程的两种方式
- Xcode9.1:swift自定义tableview下拉刷新
- C语言小游戏 三子棋
- 【Python学习之路】My Forth journey
- 自定义控件学习笔记(五)Canvas 对绘制的辅助——范围裁切和几何变换
- 安卓学习记录——(1.安卓项目的目录结构)
- QT lineedit 小疑问
- Android图片加载框架最全解析(四),玩转Glide的回调与监听
- 201711231640->unity调用安卓震动功能
- 一些java基础面试题