前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-TableView统一数据源代理

iOS-TableView统一数据源代理

作者头像
用户2215591
发布2018-08-02 16:13:23
8750
发布2018-08-02 16:13:23
举报
文章被收录于专栏:iOSer成长记录iOSer成长记录

TableView 是 iOS 应用程序中非常通用的组件,几乎每一个界面都有一个TableView,而我们许多的代码都和TableView有关系,比如数据展示、更新TableView,一些响应选择事件等,而这些大多都会通过其代理函数来实现,所以在VC中我们通常需要实现大量TableView的代理函数,如下面这样

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 12.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.01
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return nil
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

如果上面的代码在每个VC中都实现一次,不仅写了很多的重复的代码,还增加了VC的复杂度,所以我在想能不能有一个统一的代理类,我们的TableView只要遵循它,就不用每次都要写一大堆的代理方法,下面就是我写的一个代理类的使用

代码语言:javascript
复制
    private var delegate = CCDataSource()

    lazy private var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .grouped)
        // 1.注册cell
        table.register(Custom1Cell.self, forCellReuseIdentifier: "cell1")
        table.register(Custom2Cell.self, forCellReuseIdentifier: "cell2")
        // 2.代理
        table.delegate = self.delegate
        table.dataSource = self.delegate
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(tableView)
        self.setupTableView()
        self.loadData()
    }

    private func loadData() {
        // 3.网络请求数据源,并赋值
        delegate.datas = [[Model1(),Model1(),Model1()],[Model2(),Model2(),Model2(),Model2()]]
        // 4.刷新视图
        tableView.reloadData()
    }

    private func setupTableView() {
        // 在这里实现TableView的代理
        delegate.identifier { (indexPath) -> (String) in
            // 5.确定cell的类型
            return indexPath.section == 0 ? "cell1" : "cell2"
        }.headerHeight { (section) -> (CGFloat) in
            // 6.头部高度
            return 12.0
        }.footerHeight { (section) -> (CGFloat) in
            // 7.尾部高度
            return 0.01
        }.rowHeight{ (indexPath, data)  -> (CGFloat) in
            // 8.行高
            if let model = data as? Model1 {
                return model.height()
            }
            if let model = data as? Model2 {
                return model.height()
            }
            return 44.0
        }.setContentCell { (cell, data) -> (Void) in
            // 9.配置数据源
            if let item = cell as? Custom1Cell, let model = data as? Model1 {
                item.textLabel?.text = "Custom1Cell" + model.description
            }
            if let item = cell as? Custom2Cell, let model = data as? Model2 {
                item.textLabel?.text = "Custom2Cell" + model.description
            }
        }.selected {[weak self] (indexPath, data) -> (Void) in
            // 10.点击事件(这里[weak self]需要防止循环引用)
            self?.navigationController?.pushViewController(ViewController(), animated: true)
        }
    }
  1. 注册cell:这一步很重要,这个代理类只支持这种方式加载cell,你在该界面有几种cell,就需要注册几个cell类
  2. 代理: 将代理实例赋值给tableView的代理,这里我将dataSource和delegate统一为delegate了,并且如果有多个TableView,我们还可以创建多个代理实例与其一一对应
  3. 网络请求:这里是做网络请求地方,并且将请求后的数据保存在代理类中
  4. 刷新视图
  5. 确定cell的类型:cell是通过它注册identifier来创建的,所以根据indexPath来返回相应的cell注册的identifier即可
  6. 头部高度:header的高度,可以是定值,也可以根据section来动态返回
  7. 尾部高度:footer的高度,可以是定值,也可以根据section来动态返回
  8. 行高:这里的行高可以通过data来获取,这样利于做高度缓存,也可以通过indexPath来动态返回
  9. 配置数据源:这里可以获取的已经初始化号的cell和其对应的数据源,我们只需要将其赋值给cell即可
  10. 点击事件

上面这些步骤也不是固定的,这里有链式编程的思想,有些属性可以不设置则会取默认值,当然也可以重复设置,不过此时后面的会覆盖前面的 通过上面的方法,我们只需要创建一个CCDataSource实例,就可以在一个方法中将所有的TableView代理实现,而且在第5步时,我们就将cell与data对应起来了,后面会减少很多复杂的if else判断,这不仅减少了代码量,同时也使实现逻辑更加清晰

Demo地址:https://github.com/cdcyd/CCDataSource

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档