swift3使用协议和泛型简化UITableView的使用(续)多种类型的cell注册

来源:互联网 发布:python能用来做什么 编辑:程序博客网 时间:2024/06/07 01:01

1、协议

////  ReusableCell.swift//  reusecell////  Created by targetcloud on 2017/3/26.//  Copyright © 2017年 targetcloud. All rights reserved.//import UIKitprotocol ReusableCell : class{    static var reuseableIdentifier :String {get}    static var nib : UINib? {get}}extension ReusableCell where Self : UITableViewCell{    static var reuseableIdentifier :String{        //return "\(self)"        //两种实现方式都可以        print(" --- \(self)  \(String(describing: self)) --- ")        return String(describing: self)    }        static var nib : UINib? {        return nil    }}extension UITableView{    func registerCell<T : UITableViewCell >(_ cell : T.Type) where T : ReusableCell{        if let nib  = T.nib{            register(nib, forCellReuseIdentifier: T.reuseableIdentifier)        }else{            register(cell, forCellReuseIdentifier: T.reuseableIdentifier)        }    }        func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) -> T where T : ReusableCell{        return dequeueReusableCell(withIdentifier: T.reuseableIdentifier, for: indexPath) as! T    }}


2、自定义cell

////  TGNib2Cell.swift//  reusecell////  Created by targetcloud on 2017/3/26.//  Copyright © 2017年 targetcloud. All rights reserved.//import UIKitclass TGNib2Cell: UITableViewCell,ReusableCell {    static var nib : UINib?{        return UINib(nibName: "TGNib2Cell", bundle: nil)    }    }

3、使用

////  ViewController.swift//  reusecell////  Created by targetcloud on 2017/3/26.//  Copyright © 2017年 targetcloud. All rights reserved.//import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()                let tv = UITableView(frame: view.bounds)        tv.dataSource = self        tv.registerCell(TGCell.self)        tv.registerCell(TGNibCell.self)        tv.registerCell(TGNib2Cell.self)        view.addSubview(tv)    }}extension ViewController :UITableViewDataSource{    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return 20    }        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {                if indexPath.row % 2 == 1 {            let cell = (tableView.dequeReusableCell(indexPath: indexPath) as TGNibCell)            cell.nibTestLabel?.text = " --- ReusableCellFromNib \(indexPath.row) --- "            return cell        }                if indexPath.row % 3 == 2 {            let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell            cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- "            return cell        }                let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGNib2Cell        return cell    }}

多种cell类型同时注册后的代码执行过程

先是系统注册三种类型的cell,会打印三次类型,分别为

 --- TGCell  TGCell --- 

 --- TGNibCell  TGNibCell --- 

 --- TGNib2Cell  TGNib2Cell --- 


然后是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

当它调用dequeReusableCell来到了

func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) -> T where T : ReusableCell

当它需要确定何种类型的cell时,来到了

static var reuseableIdentifier :String

得到了类型后再原路返回cell


前三次的代码执行又会返回以下结果

 --- TGNib2Cell  TGNib2Cell --- 

 --- TGNibCell  TGNibCell --- 

 --- TGCell  TGCell --- 


执行效果




0 0