Swift4中利用runtime实现UITableViewCell动态加载

在开发中遇到一个列表有多种cell样式的时候,比如今日头条首页,利用if 来判断可能要写死,这个时候就可以利用反射来决定加载哪一种cell,但是不管怎么,cell的类型是提前写好的,不存在绝对的动态cell,cell的类型可以通过后台来返回,也可以根据后台返回的内容来决定加载哪一种cell
首先来定义一个基类:

class QSBaseTableViewCell: UITableViewCell {
    
    //得到重用标识符
    static func getIdentifier() -> String {
        let identifier = NSStringFromClass(self)
        return identifier
    }

    //如果想用元类型初始化,必须要用required标记初始化方法
    required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

想利用反射初始化一个类的话,就必须指定初始化方法为required
这里模拟两个子类:

class QSFirstTableViewCell: QSBaseTableViewCell {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }
    
    required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.blue
    }
        
}

class QSSecondTableViewCell: QSBaseTableViewCell {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.red
    }
    
}

两个cell子类,继承基类,在初始化方法里设置自己的UI
然后我们来模拟一个数据源:

        //构造数据源
        for i in 1...50 {
            
            let bound = UInt32(i)
            let temp = Int(arc4random_uniform(bound))
            if temp % 2 == 0{
                self.dataArr.append(QSSecondTableViewCell.getIdentifier())
            }else{
                self.dataArr.append(QSFirstTableViewCell.getIdentifier())
            }
            
        }

这个数据源里只有一个字符串,在实际应用中,这里应该是一个model,model里有个字段,是cell的类型名,这个类型名可以是后台返回的,也可以根据后台返回内容来返回一个计算属性
接下来就是返回cell的代理了

extension ViewController: UITableViewDelegate, UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellStr = self.dataArr[indexPath.row]
        let celltype = NSClassFromString(cellStr) as! QSBaseTableViewCell.Type
    
        var cell = tableView.dequeueReusableCell(withIdentifier: cellStr)
        
        if cell == nil {
            cell = celltype.init(style: .default, reuseIdentifier: cellStr)
        }
        
        return cell!
    }
}

在返回cell的代理里,我们通过数据源,得到cell的类型名,再通过反射得到cell的元类型,然后我们就可以创建一个cell了。这样我们就实现了根据model类型来决定cell类型的功能。在添加model到数据源的时候,我们需要过滤一下,以防后台返回的数据里出现了没有定义的cell类型,出现崩溃或者UI的bug。
demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • 1从本篇文章中我学到的最重要的概念 每个人都有梦想,但梦想只是单纯的梦想,是一个人想倾尽一切想达成的事,不能夹杂功...
    110尹月琴阅读 365评论 3 1
  • 放个二维码
    初公瑾阅读 543评论 0 0
  • Core new Promise(Function<Function resolve, Function reje...
    sshmyeclipse阅读 326评论 1 1
  • 虚拟主机 什么是虚拟主机 实现一台主机,对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。 怎么样实现虚...
    lqsss阅读 1,501评论 0 0