美文网首页
控制器减负之分离数据源

控制器减负之分离数据源

作者: barry | 来源:发表于2017-04-17 11:30 被阅读30次

Tableview在工程中使用频率非常高,那么对应的数据源也会频繁的出现的工程中。可以将这部分分离到单独的类中 ( 这个类可以复用到工程中 ) ,这样控制就可以减少一部分的代码量。闲话少说,上代码:###

DataSource中的数据源方法


   //MARK: -UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return itemsArray!.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return itemsArray![section].count
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: identifierArray![indexPath.section] )!
        
        //获取相应的模型数据
        let item = itemAt(indexpath: indexPath)
        
        //执行回调
        cellBlock!(cell, item, indexPath)
        
        return cell

    }

TableView中设置数据源代码

    //MARK: - lazy load tableView
    lazy var tableView:UITableView = {
        let tableView:UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .grouped)
        
        var cellOneData = [AnyObject]()
        
        var cellTwoData = [AnyObject]()
        
        for i in 0..<5 {
            cellOneData.append("我是组一中第\(i)行的数据" as AnyObject)
        }
        
        for i in 0..<5 {
            cellTwoData.append("我和组一长的很像但我是组二" as AnyObject)
        }
        
        //分离数据源
        self.dataSource = DataSource(itemsArray: [cellOneData, cellTwoData], identifiers: [self.tableCellOneID,self.tableCellTwoID], cellBlock: { (cell, item, indexPath) in
            
            //TableViewCellOne和TableViewCellTwo就是简单的继承了一下UITableViewCell
            if indexPath.section == 0 {
                cell.textLabel?.text = item as? String
            } else {
                cell.textLabel?.text = item as? String
            }
            
        })
        
        //直接把对象赋值给tableView.dataSource不行,没有保存对象
        tableView.dataSource = self.dataSource
        
        //注册两个cell
        tableView.register(TableViewCellOne.self, forCellReuseIdentifier: self.tableCellOneID)
        
        tableView.register(TableViewCellTwo.self, forCellReuseIdentifier: self.tableCellTwoID)
        
        return tableView
    }()

Github地址:DataSource代码

相关文章

  • 控制器减负之分离数据源

    Tableview在工程中使用频率非常高,那么对应的数据源也会频繁的出现的工程中。可以将这部分分离到单独的类中 (...

  • UICollectionView 基本使用

    控制器继承UICollectionViewController 它自动遵守数据源 和 代理了。 1.实现数据源方法...

  • UITableView

    tableView展示数据 设置数据源对象(一般是控制器) 数据源对象需要遵守协议->UITableViewDat...

  • sharding-jdbc 4.x版本bug记录

    分库分表数据源不支持ON DUPLICATE KEY UPDATE写法 读写分离数据源测试支持 ISSUE:htt...

  • 读写分离实现

    读写分离 经典模式 思路: 1.准备多个数据源 2.把所有的数据源存入AbstractRoutingDataSou...

  • 利用spring AOP特性,动态切换数据源

    以读写分离为例. 主数据源master负责写入,从数据源cluster负责读取 以上个项目为基础,添加如下依赖 a...

  • UITableView分离数据源

    理论上来说,就是在Viewcontroller中分离出去table和table的数据源,把它们的功能更加细化,方便...

  • UITableViewCell嵌套UICollectionVie

    UITableView和UICollection的嵌套使用1.在控制器中创建TableView,设置数据源和代理 ...

  • OC:抽出数据源代理

    前言: 为控制器瘦身,本文提供一个抽出数据源代理的示例,具体请结合自身的需求定制。抽出数据源代理不是必须的,如果控...

  • angular的知识

    Angular: MVC 分离: 基本用法: Angular特性: Angular: 控制器: Controll...

网友评论

      本文标题:控制器减负之分离数据源

      本文链接:https://www.haomeiwen.com/subject/tuftzttx.html