GPJDataDrivenTableView 0.6.2

GPJDataDrivenTableView 0.6.2

Gong Pengjun维护。



GPJDataDrivenTableView

描述

GPJDataDrivenTableView 是一种使用 UITableView 的数据驱动方式。

GPJDataDrivenTableView 具有以下功能

  • 双向数据绑定(映射)
    • 基于它们的类名在 XXXData 上绑定 XXXCell 的 UI 元素
    • 通过 GPJTableViewData.didSelectAction 块从 XXXCell 向 XXXData 发送动作
  • 接口直观,因此易于使用
    • 使用 GPJTableViewData 的各种子类构造 dataArray
    • dataArray 传递给 -[GPJDataDrivenTableView reloadDataArray:]

GPJDataDrivenTableView 具有以下优点

  • 通过细胞类型解耦代码,因此我们获得了非常细粒度的代码解耦
    • XXXCell 和 XXXData 位于 XXXData.h/.m
    • YYYCell 和 YYYData 位于 YYYData.h/.m
  • 业务代码中不存在 IndexPath,因此消除了索引周围的错误

然后,我们可以独立地添加/删除/修改单元格

  • 添加一个新的单元格类型或新的单元格实例不会影响其他人
  • 删除一个单元格类型或单元格实例不会影响其他人
  • 修改一个单元格类型或单元格实例不会影响其他人

最终,我们的代码可以与需求的变化和谐地发展。🎉🎉🎉Woohoo🎉🎉🎉

用法

#import <GPJDataDrivenTableView/GPJDataDrivenTableView.h>

GPJDataDrivenTableView *dataDrivenTableView = [[GPJDataDrivenTableView alloc] initWithFrame:self.view.bounds];
dataDrivenTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:dataDrivenTableView];

NSMutableArray *dataArray = [NSMutableArray array];
{
	ActionData *actionData = [ActionData new];
	actionData.didSelectAction = ^(id data) {
	    [weakSelf actionCellReloadAction:data];
	};
	[dataArray addObject:actionData];
}
{
	ColorData *data = [ColorData new];
	data.didSelectAction = ^(id data) {
	    [weakSelf colorCellAction:data];
	};
	[dataArray addObject:data];
}
[dataDrivenTableView reloadDataArray:dataArray];

需求

GPJDataDrivenTableView适用于iOS 6及以上版本,并且需要ARC进行编译。

安装

CocoaPods

pod 'GPJDataDrivenTableView'

手动

  1. 下载GPJDataDrivenTableView仓库
  2. 将GPJDataDrivenTableView子文件夹复制到您的Xcode项目中

例子

screenshot_basic_small

screenshot_flex_1_small screenshot_flex_2_small screenshot_flex_3_small screenshot_flex_4_small

screenshot_edit_1_small screenshot_edit_2_small screenshot_edit_3_small screenshot_edit_4_small

来源

传统方法是索引驱动的,我们通过实现UITableViewDataSource的或UITableViewDelegate的方法基于

  • -tableView:cellForRowAtIndexPath
  • -tableView:heightForRowAtIndexPath
  • -tableView:didSelectRowAtIndexPath

这种基于

uitableview_indexdriven

新的方法是数据驱动的。GPJDataDrivenTableView将自己设置为UITableView的数据源和代理,并在其dataArray中以合适和安全的方式将indexPath映射到data。我们只需要构造dataArray并调用reloadDataArray:,就是这样。

  • 使用 GPJTableViewData 的各种子类构造 dataArray
  • dataArray 传递给 -[GPJDataDrivenTableView reloadDataArray:]

uitableview_datadriven

使用数据驱动方式,我们不需要关注indexPath。它是无错误的,容易适应需求的变化。

实现选择和详细说明

在比较了组合子类分类实现后,我选择了子类实现。

有三个类:GPJDataDrivenTableView、GPJTableViewCell和GPJTableViewData

  • GPJDataDrivenTableView:UITableView的子类
    • 它实现了UITableViewDataSource和UITableViewDelegate
    • 它有一个dataArray属性来保存GPJTableViewData子类的实例
    • 它使用名称字符串替换将XXXCell绑定到XXXData
    • 它响应tableView:didSelectRowAtIndexPath:并调用GPJTableViewData.didSelectAction
    • 它隐藏dataSource / delegate以防止错误使用
    • 它公开gpjDataSource / gpjDelegate以扩展和自定义
  • GPJTableViewCell:UITableViewCell的子类,实现单元格UI
  • GPJTableViewData:NSObject的子类,实现-cellHeight方法以指定单元格高度。

代码库只有大约两行,不妨看看。GPJDataDrivenTableView.m

希望你会喜欢。