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为我们自定义组件的名字。

原创粉丝点击