JDTableView 0.0.8

JDTableView 0.0.8

wjd 维护。



  • wangjindong

JDTableView

在 iOS 项目中,tableview 可能是使用最频繁的控件之一,但如果你还在自己实现 tableview 的 delegate 和 datasource,那就有点不够Cool了。尤其是当 delegate 和 datasource 的逻辑几乎相同的时候。本项目就是为了处理一个数组而抽象实现掉 delegate 和 datasource,甚至可以完全依靠一个数组源来处理 tableview 的渲染和事件处理。

这种写法也有助于开发者只关注数据和界面,无需考虑 tableview 的渲染逻辑。

通过利用 runtime 的 class_addMethod 向 delegate 和 dataSource 动态添加实现协议的方法,即使不实现任何方法也可以展示数据。因为在动态添加之前,会判断 delegate 和 dataSource 中是否已有该方法,所以你可以根据自己的喜好来实现!

-------惯例先上图,图丑但是它不是重点------

此处的效果图实现非常简单,无需实现一行协议代码。

设计理念:动态添加委托方法,实现 tableview 的委托,无需程序员实现任何接口方法,所有可能来源均有数据结构(即数组)提供!

该方法支持在数据中添加 tableview 的选择事件,默认的 cell 样式、accessoryView、block 等等!

使用就2步

第一步:设置委托类:self.tableView.jd_delegate = self;

第二步:设置数据源:self.tableView.jd_dataSource = self;

第三步:制作配置表,比如配置 cell 的数组,数据对应的 cell 数组的索引。

第四步:构造数据源,将获取的数据交由 JDViewModel 进行管理。

例如:

- (void)viewDidLoad {
    [super viewDidLoad];
    //第一步:设置委托类
    self.tableView.jd_delegate = self;
    //第二步:设置数据源
    self.tableView.jd_dataSource = self;
    //第三步:配置tableView
    [self configTableView];
    //第四部:加工数据源
    [self configDataSource];
  }
 
 
  - (void)configTableView {
    JDTableViewConfig *config = [[JDTableViewConfig alloc] init];
    config.tableViewCellArray = @[[UINib nibWithNibName:@"DemoTableViewCell1" bundle:nil],
                                  [UINib nibWithNibName:@"DemoTableViewCell2" bundle:nil]
                                ];
   //配置数据源与cell的对应关系
    config.cellTypeBlock = ^NSInteger(NSIndexPath *indexPath, id dataInfo) {
        return 0;
    };
    
    //配置都有哪些header
    config.tableViewHeaderViewArray = @[[FirstTableViewHeaderFooterView class]];
    //配置数据源与header的对应关系
    config.headerTypeBlock = ^NSInteger(NSUInteger section, id sectionInfo) {
        return 0;
    };
    
    ////////////////////////编辑////////////////////////
    config.canEditable = ^BOOL(NSIndexPath *indexPath) {
        return YES;
    };

    config.singleLineDeleteAction = ^(NSIndexPath *indexPath) {
        NSLog(@"我要删除第%ld行",indexPath.row);
    };
    self.tableView.jd_config = config;
  }

  - (void)configDataSource {
    self.tableViewModel = [[JDViewModel alloc] init];
    self.tableView.jd_viewModel = self.tableViewModel;
    
    for (NSInteger i = 0; i < 4; i++) {
        //开始组织对象
        JDSectionModel *section = [[JDSectionModel alloc] init];
        //section1.title = @"TableView";
        section.sectionData = [NSString stringWithFormat:@"我是自定义数据%ld",i];
        //section也可以配置数据源与cell的对应关系,它的优先级高于config的配置
        section.cellTypeBlock = ^NSInteger(NSIndexPath *indexPath, id dataInfo) {
            return [dataInfo[@"type"] integerValue];
        };
        NSDictionary *data = [DataUtils dataFromJsonFile:@"first.json"];
        [section addRowDatasFromArray:data[@"items"]];
        [self.tableViewModel addSectionData:section];
    }
  }

如果您有好的建议,请联系我:[email protected],其实自己仔细研究更有意思!

CocoaPods

1、在 Podfile 中添加 pod 'JDTableView'

2、执行 pod installpod update

3、导入 <JDTableView/JDTableView.h>