zoukankan      html  css  js  c++  java
  • IOS之未解问题--给UITableView提取UITableViewDataSource并封装瘦身失败

    前言:阅读了《更轻量的 View Controllers》,发现笔者这个优化重构代码的想法真的很不错,可以使得抽取的UITableViewDataSource独立写在一个类文件里,并且也写出了了自定义UITableViewCell绑定相关的xib,然后在类别拓展这个UITableViewCell然后赋值。抽取UITableViewDataSource的亮点就是将过程"用model的属性设置cell"通过block来写入并进行了传值。

    但是小编自己想进一步封装,想达到这个UITableViewDataSource类,能够达到完全独立和封装,提供一个方便用的接口来使用。

    但是没有成功,还是留给以后捣鼓捣鼓吧,对应《更轻量的 View Controllers》的github下载地址:github上的示例程序。

    下面是我自己的简要分析过程,试图将UITableViewDataSource进行抽取封装:

    UITableViewDataSource任务执行的过程
    
    
    首先将UIViewController对象设置为 UITableView对象的 数据源
    
        self.tableView.dataSource = self;
    
    或者将UITableView的子类本身设置为 自己的 数据源
    
        self.dataSource = self;
    
    然后实现数据源协议的方法
    
    #pragma mark - UITableViewDataSource协议方法
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.visibleCellRows.count;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        NSArray* arr =  self.visibleCellRows[section];
        return arr.count;
    }
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return @"Hello";
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
                                                                forIndexPath:indexPath];
        id item = [self itemAtIndexPath:indexPath];
        self.configureCellBlock(cell, item);
        return cell;
    }
    
    
    但是:
        要先拿到模型对象
    因为:
        要用到模型对象的count
    
        模型对象的一些数据:
            UITableViewCell的创建和设置,绑定ID的形式
            对于二维模型数组,先要获得section的title,section的高
    
    
    
    数据源内部的方法:
        可以到apple内部头文件查看
    
    ==================================================
    
    而封装的需求:
        参数:1、模型数组 2、cell的ID 3、
        业务:1、用模型属性为cell的属性进行赋值
    
    
    ===================================================
    文章:http://www.objccn.io/issue-1/ 
            中是将业务1的逻辑步骤 写在自定义cell的类别中
  • 相关阅读:
    MySQL常用函数及逻辑运算
    博客主题2
    TCPIP详解第1卷1.3TCPIP分层1.4互联网的地址1.5域名系统1.6封装
    删除数组中满足特定需求的数字
    自定义简洁浏览器主页
    Matlab图像处理函数:regionprops
    连通域的质心
    Matlab的GUI参数传递方式总结
    retrifit
    association ,collection
  • 原文地址:https://www.cnblogs.com/goodboy-heyang/p/5110165.html
Copyright © 2011-2022 走看看