cocbin

2个pod

CBCategoryView

实例化 CBCategoryView

CBCategoryView * categoryView = [[CBCategoryView alloc] initWithPosition:CGPointMake(0,64) andHeight:40]; [self.view addSubview:categoryView];

添加数据

[categoryView dataSource:self.category]; // self.category 是一个字符串数组

使用block设置切换tab时使用的ViewController

[categoryView controller:self getChildViewController:^(CBCategoryView * cbCategoryView, NSInteger index) { ChildViewController * childViewController = [[ChildViewController alloc] init]; childViewController.param = self.category[(NSUInteger) index]; return childViewController; }];

许可证: MIT

  • Objective C

CBTableViewDataSource

CBTableViewDataSource

[演示](media/demo.jpg)

只需一行代码即可为 UITableView 创建 DataSourceDelegate中文文档 (中文文档)

简介

CBTableViewDataSource 是一个轻量级的框架,用于快速创建 UITableViewDataSourceDelegate。它提供简单API来创建逻辑清晰且易于维护的代码。

创建类似这样的 DataSource 是最懒惰的方法

objective-c [_tableView cb_makeSectionWithData:self.viewModel.data andCellClass:[CustomCell class]];

当然,您必须遵循这种方式的一些约定。同时,我也提供了其他灵活的方式来创建 DataSource

详情请见以下文档。

为何使用

我们在开发App时,总是花费大量的时间和精力为 UITableView 创建 DataSourceDelegate。由于这些代码倾向于重复且难以维护,因为它们位于每个方法中。我们必须从各个角落找到它们,并在维护程序时进行修改。

然而,CBTableViewDataSource 改变了这一切,提供一个简单的API帮助我们创建逻辑清晰且易于维护的代码。

为了让大家注意到该框架的优点,让我们来做一番比较。

以下原生方式

``` objective-c

// 原生视图

// 定义一个enum来分割section

typedef NSENUM(NSInteger, SectionNameDefine) { SECTIONONE, SECTIONTWO, SECTIONTHREE, SECTIONFOUR, //... COUNTOFSTORESECTION };

// 定义section的标识符

define IDENTIFIERONE @"IDENTIFIERONE"

define IDENTIFIERTWO @"IDENTIFIERTWO"

define IDENTIFIERTHREE @"IDENTIFIERTHREE"

define IDENTIFIERFOUR @"IDENTIFIERFOUR"

//...

// 为section注册cell类

[self.tableView registerClass:[OneCell class] forCellWithReuseIdentifier:IDENTIFIERONE]; [self.tableView registerClass:[TwoCell class] forCellWithReuseIdentifier:IDENTIFIERTWO]; [self.tableView registerClass:[ThreeCell class] forCellWithReuseIdentifier:IDENTIFIERTHREE]; [self.tableView registerClass:[FourCell class] forCellWithReuseIdentifier:IDENTIFIERFOUR];

// 实现dataSource协议

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return COUNTOFSTORE_SECTION; }

  • (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section { return ((NSArray)self.data[section]).count; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger section = (NSUInteger) indexPath.section; NSUInteger index = (NSUInteger) indexPath.row; switch(section) { case SECTIONONE: // to do something return cell; case SECTIONTWO: // to do something return cell; case SECTIONTHREE: // to do something return cell;

        //...
    

    }

    return cell; } // ...

```

使用这种方式非常繁琐且难以维护。

而使用 CBTableViewDataSource

``` objective-c [tableView cbmakeDataSource:^(CBTableViewDataSourceMaker * make) { // section one [make makeSection:^(CBTableViewSectionMaker *section) { section.cell([OneCell class]) .data(self.viewModel.oneData) .adapter(^(OneCell * cell,id data,NSUInteger index){ [cell configure:data]; }) .autoHeight(); }]; // section two [make makeSection:^(CBTableViewSectionMaker *section) { section.cell([TwoCell class]) .data(self.viewModel.twoData) .adapter(^(FeedCell * cell,id data,NSUInteger index){ [cell configure:data]; }) .autoHeight(); }];

// ... so on

}]; ```

它简洁而层次分明。最重要的是它能更好地使代码符合编程者的思维。

用法

安装

使用 cocoapods

ruby pod 'CBTableViewDataSource'

导入

``` objective-c

导入

```

创建 DataSourceDelegate

``` objective-c [tableView cbmakeDataSource:^(CBTableViewDataSourceMaker * make) { // section one [make makeSection:^(CBTableViewSectionMaker *section) { section.cell([OneCell class]) .data(self.viewModel.oneData) .adapter(^(OneCell * cell,id data,NSUInteger index){ [cell configure:data]; }) .autoHeight(); }]; // section two [make makeSection:^(CBTableViewSectionMaker *section) { section.cell([TwoCell class]) .data(self.viewModel.twoData) .adapter(^(FeedCell * cell,id data,NSUInteger index){ [cell configure:data]; }) .autoHeight(); }];

// ... so on

}]; ```

示例

仅使用数据

objective-c UITableView tableView = [UITableView new]; [tableView cb_makeSectionWithData:data];

这样将以默认的 UITalbeViewCell 作为单元格类。

数据必须遵循以下规范:

  1. 数据是 NSArray (NSArray < NSDictionary * >*)。
  2. 字典的键如下

    • text 用于为 UITableViewCell 的 textLabel 设置文本
    • detail 用于为 UITableViewCell 的 detailTextLabel 设置文本
    • value 用于为 UITableViewCell 的 detailTextLabel 设置文本
    • image 用于为 UITableViewCell 的 imageView 设置图像
    • accessoryType 用于为 UITableViewCell 设置访问类型

    valuedetail 都可用来为 UITableViewCell 的 detailTextLabel 设置文本。如果使用 detail 作为键,detailTextLabel 将显示在 textLabel 的底部。如果使用 value 作为键,detailTextLabel 将显示在 textLabel 的右侧。同时不要使用两者,并且数组中第一个出现的将具有优先级。

例如:

objective-c _data = @[ @{@"text":@"Following",@"value":@"45"}, @{@"text":@"Follower",@"value":@"10"}, @{@"text":@"Star",@"value":@"234"}, @{@"text":@"Setting",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator)}, @{@"text":@"Share",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator)}];

UI如下

请参考名为 DemoTwoViewController.hDemoTwoViewController.m 的文件以了解详细信息。

使用自定义单元格

objective-c [tableView cb_makeSectionWithData:data andCellClass:[CustomCell class]];

CustomCell 必须提供 Configuer: 方法或 Configuer:index: 方法以适应数据。

例如

``` objective-c - (void)configure:(NSDictionary *)row index:(NSNumber * )index { if (row[@"avatar"]) { [self.avatarView setImage:[UIImage imageNamed:row[@"avatar"]]]; } else { [self.avatarView setImage:nil]; } [self.nameLabel setText:row[@"name"]]; [self.titleLabel setText:row[@"title"]]; [self.detailLabel setText:row[@"detail"]]; self.circleView.hidden = row[@"unread"] == nil;

if([index intValue] &1) {
    self.contentView.backgroundColor = [UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
} else {
    self.contentView.backgroundColor = [UIColor whiteColor];
}

} `` 请参考名为 CustomCell.h 和 CustomCell.m 的文件。

UI如下

请参考名为 DemoOneViewController.hDemoOneViewController.m 的文件。

更灵活的设置

objective-c [tableView cb_makeSection:^(CBTableViewSectionMaker * section) { section.data(@[]); section.cell([CustomCell class]); section.adapter(^(CustomCell cell,id row,NSUInteger index) { cell.configure(row); }); section.event(^() { // do something }) // 其他设置 }];

这里展示的是单个节段的示例。

CBTableViewSectionMaker

CBTableViewSectionMaker 用于设置节段的某些属性。以下是一些可用的属性:

data

设置在 UITableView 中显示的数据,必需参数是一个 NSArray。

例如

objective-c section.data(@[@(goods1),@(goods2),...]);

cell

设置用于显示数据的 Cell Class。单元格的标识符将自动注册。

例如

objective-c section.cell([CustomCell class]);

adapter

用于适配单元格和日期。

例如

objection-c section.adapter(^(CustomCell * cell,id row,NSUInteger index) { [cell configure:row]; // ... });

event

用于设置当单元格被触摸时的事件,例如

objective-c section.event(^(NSUInteger index,id row) { CustomViewController * controller = [CustomViewController new]; controller.viewModel.data = row; [self.navigationController pushViewController:controller animated:YES]; });

height

用于设置单元格的高度。这是一个静态值。这个高度只适用于当前节段。

objective-c section.height(100);

autoHeight

用于设置单元格的动态计算高度。

objective-c section.autoHeight();

如果设置了 autoHeight,则 height 将无效。

headerTitle;

用于设置节段的标题。例如

objective-c section.headerTitle("title");

footerTitle;

用于设置节段的页脚标题。类似。

headerView;

用于设置部分的头部视图。例如

objective-c section.headerView(^(){ UIView * headerView = [UIView alloc]initWithFrame:CGRectMake(0,0,320,40); // ... return headerView; })

如果设置了headerView,则headerTitle将无效。

footerView;

用于设置部分的底部视图。类似。

多部分

objective-c [tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) { [make headerView:^{ return [HeaderView new]; }]; [make makeSection: ^(CBTableViewSectionMaker * section) { section.data(@[]); section.cell(); section.adapter(); section.event(); // ... so on }]; [make makeSection: ^(CBTableViewSectionMaker * section) { section.data(@[]); section.cell(); section.adapter(); section.event(); // ... so on }]; [make makeSection: ^(CBTableViewSectionMaker * section) { section.data(@[]); section.cell(); section.adapter(); section.event(); // ... so on }]; // ... [make footView:^{ return [FooterView new]; }]; }]

UI如下

请在名为 DemoThreeViewController.hDemoThreeViewController.m 的文件中查看详细信息。

CBTableViewDataSourceMaker

CBTableViewDataSourceMaker用于为UITableView设置一些属性。以下是一些可用的属性

makeSection

用于为UITableView添加部分。例如

objective-c [tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) { [make makeSection: ^(CBTableViewSectionMaker * section) { // ... } }]

height

用于设置UITableView的默认高度

objective-c make.height(100);

如果设置了部分的heightautoHeight,此处的高度将无效。默认为40。

headerView

用于设置UITableView的表头视图。注意tableHeaderView和部分headerView之间的差异。

例如

objective-c make.headerView(^(){ UIView * headerView = [[UIView alloc]init]; // ... return headerView; });

footerView

用于设置UITableView的表脚视图。类似。

commitEditing

用于为UITableViewDelegate设置commitEditing方法。

objective-c [make commitEditing:^(UITableView * tableView, UITableViewCellEditingStyle * editingStyle, NSIndexPath * indexPath) { // do somethings. }];

scrollViewDidScroll

用于为UITableViewDelegate设置scrollViewDidScroll方法

objective-c [make scrollViewDidScroll:^(UIScrollView * scrollView) { // do somethings }];

感谢

感谢您使用并支持。欢迎提出问题和请求Pull Request。我将优先处理。

我在这个框架中参考了许多大师。例如,我在设计API时参考了著名的autolayout框架Masonary。动态计算cell高度的方 法参考了。@forkingdog's UITableView-FDTemplateLayoutCell.

感谢他们给我带来灵感。

通过电子邮件联系我:

许可证: MIT

  • Objective C