Swift使用泛型进行快速的 UITableViewCell 复用

UITableView是我们经常使用的,通常我们的 cell 复用是按照以下的方法进行的
class CustomCell: UITableViewCell {  
}

class ViewController: UITableViewController {

    let cellIdentifier = "let cellIdentifier"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(CustomCell.self, forCellReuseIdentifier: cellIdentifier)
    }
}

extension ViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
        return cell
    }
}
这样做有几个缺点
  1. 需要声明cellIdentifier, 保证不重复,且使用 xib/storyboard 时需要保证一致性,容易出错
  2. 返回的 cell 类型需要进行强制类型转换
  3. 多人协作命名一致性问题

由于类名是不能重复的,为了解决这些问题,在最开始的时候,使用类名作为 cellIdentifier, 减少出现问题的可能,保证多人协作的一致性,像下面这样

let cellIdentifier = "CustomCell"
但是这并不方便,使用起来也基本没有太大的差别,再次尝试使用 UITableViewCell 类型直接进行注册,直接使用类型进行复用,方法也很简单,获取类名进行注册复用即可,方法如下
// 给 UITableView 进行方法扩展,增加使用类型进行注册和复用的方法
extension UITableView {
    func register(_ cellClass: UITableViewCell.Type) {
        let identifier = String(describing: cellClass)
        register(cellClass, forCellReuseIdentifier: identifier)
    }
    func dequeueReusableCell(with cellClass: UITableViewCell.Type, for indexPath: IndexPath) -> UITableViewCell {
        let identifier = String(describing: cellClass)
        return dequeueReusableCell(withIdentifier: identifier, for: indexPath)
    }
}
目前还剩最后一个问题,解决复用返回 cell 需要进行强制类型转换的问题, 这里用到了泛型来解决这个问题
extension UITableView {
    func dequeueReusableCell<T: UITableViewCell>(with cellClass: T.Type, for indexPath: IndexPath) -> T {
        let identifier = String(describing: cellClass)
        return dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! T
    }
}
这样就完成了整个的改造工作,用起来也非常简单
class CustomCell: UITableViewCell {
    var index = 0
}

class ViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(CustomCell.self)
    }
}

extension ViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(with: CustomCell.self, for: indexPath)
        cell.index = indexPath.row
        return cell
    }
}

结语

  1. 存在问题:在 storyboard/xib 上使用需要复制类名作为 identifier,目前没找到好的解决办法
  2. 这里只举例了注册 UITableViewCell 复用的情况,其实头尾视图的注册复用, 以及 UICollectionView 相关的复用都可以使用同样的方法进行处理,自己动手尝试一下吧
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文的重点并不仅是UITableView的基本使用方法,而是强调有关UITableView和UITableView...
    ac3阅读 38,201评论 17 51
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,211评论 30 472
  • UITableViewCell如果在tableView:cellForRowAtIndexPath:方法中,像其他...
    ForeverYoung21阅读 6,003评论 3 16
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 时逢三月,凉风从太阳上吹下,仍冰寒刺肌,却温润入骨。万物渐由冰冷转向温情,在料峭冬风中挺过来的,谁也不想在期待中的...
    私仁阅读 286评论 0 1