swift3使用协议和泛型简化UITableView的使用

来源:互联网 发布:马东眼袋在哪割的 知乎 编辑:程序博客网 时间:2024/06/05 19:31

普通我们使用UITableView时我们总是要定义一个ID,然后注册


tableview.register(cellClass: AnyClass?, forCellReuseIdentifier: String)

tableView.dequeueReusableCell(withIdentifier: String, for: IndexPath)


定义、注册、使用时我们会用到三次CellReuseIdentifier我们通过用协议和泛型简化UITableView的使用,即不再需要这三次的Identifier


实现步骤

1、写协议


//

//  ReusableCell.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


protocol ReusableCell : class{

    static var reuseableIdentifier :String {get}

}


extension ReusableCell where Self : UITableViewCell{

    static var reuseableIdentifier :String{

        return "\(self)"

    }

}


extension UITableView{

    func registerCell<T : UITableViewCell >(_ cell :T.Type)where T : ReusableCell{

        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、自定义CellCell遵守上面的协议


//

//  TGCell.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


class TGCell: UITableViewCell,ReusableCell {


}




3、使用


//

//  ViewController.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        

        let tv = UITableView(frame: view.bounds)

        tv.dataSource = self

        tv.registerCell(TGCell.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 {

        let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell

        cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- "

        return cell

    }

}


从上面看到,我们不需要再关心ID


当然协议还要能够适应XIB的注册,那么新的协议如下代码

1、修改协议


//

//  ReusableCell.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


protocol ReusableCell : class{

    static var reuseableIdentifier :String {get}

    static var nib : UINib? {get}

}


extension ReusableCell where Self : UITableViewCell{

    static var reuseableIdentifier :String{

        //return "\(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、自定义NIB(或者叫XIB


//

//  TGNibCell.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


class TGNibCell: UITableViewCell,ReusableCell {

    @IBOutlet weak var nibTestLabel: UILabel!

    

    static var nib : UINib?{

        return UINib(nibName: "TGNibCell", bundle: nil)

    }

    

}




3、使用


//

//  ViewController.swift

//  reusecell

//

//  Created by targetcloud on 2017/3/26.

//  Copyright © 2017 targetcloud. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        

        let tv = UITableView(frame: view.bounds)

        tv.dataSource = self

        //tv.registerCell(TGCell.self)

        tv.registerCell(TGNibCell.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 {

        /*

         let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell

         cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- "

        */

        let cell = tableView.dequeReusableCell(indexPath: indexPath)as TGNibCell

        cell.nibTestLabel?.text = " --- ReusableCellFromNib \(indexPath.row) --- "

        return cell

    }

}




0 0
原创粉丝点击