blob: eb813d4808ed42f607b9fddfefe3f484b702fee5 [file] [log] [blame]
{
"name": "CBTableViewDataSource",
"version": "1.1.0",
"summary": "An elegant style of writing for UITableViewDataSource",
"description": "# CBTableViewDataSource\n![demo](media/demo.jpg)\n\nJust one line to create `DataSource` and `Delegate` for `UITableView`.\n [中文文档 (Document in Chinese)](https://github.com/cocbin/CBTableViewDataSource/blob/master/README_ZH.md)\n\n## Introduction\n\n`CBTableViewDataSource` is a lightweight Framework which was used to create `DataSource` and `Delegate` for `UITableView` quickly. It provides a simple API to create logical and easily maintained code.\n\nThe most lazy way to create `DataSource` like this:\n\n``` objective-c\n[_tableView cb_makeSectionWithData:self.viewModel.data andCellClass:[CustomCell class]];\n```\n\nOf course, you must follow some convention in this way. At the same time, I also provides others flexible way to create `DataSource`.\n\nDetails as document below.\n\n## Why use\n\nWe always spend a lot of time and energy to create `DataSource` and `Delegate` for `UITableView` when we develop an App. While those code tend to repetitive and hard maintenance, because them located in each position of each delegate method. We must found them from corner to corner, and modified them when we maintain program.\n\nHowever, `CBTableViewDataSource` changed all this, and provides a simple API to help us create logical and easily maintained code.\n\nIn order to make everyone notice advantages of this framework, let's do a compare.\n\nNative way below:\n\n``` objective-c\n\n// Native vision\n\n// define a enum to split section\n\ntypedef NS_ENUM(NSInteger, SectionNameDefine) {\n SECTION_ONE,\n SECTION_TWO,\n SECTION_THREE,\n SECTION_FOUR,\n //...\n COUNT_OF_STORE_SECTION\n};\n\n// define identifier for section\n\n#define IDENTIFIER_ONE @\"IDENTIFIER_ONE\"\n#define IDENTIFIER_TWO @\"IDENTIFIER_TWO\"\n#define IDENTIFIER_THREE @\"IDENTIFIER_THREE\"\n#define IDENTIFIER_FOUR @\"IDENTIFIER_FOUR\"\n//...\n\n\n// register cell class for section\n\n[self.tableView registerClass:[OneCell class] forCellWithReuseIdentifier:IDENTIFIER_ONE];\n[self.tableView registerClass:[TwoCell class] forCellWithReuseIdentifier:IDENTIFIER_TWO];\n[self.tableView registerClass:[ThreeCell class] forCellWithReuseIdentifier:IDENTIFIER_THREE];\n[self.tableView registerClass:[FourCell class] forCellWithReuseIdentifier:IDENTIFIER_FOUR];\n\n\n// implementation datasource protocol\n\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {\n return COUNT_OF_STORE_SECTION;\n}\n\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {\n return ((NSArray*)self.data[section]).count;\n}\n\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {\n NSUInteger section = (NSUInteger) indexPath.section;\n NSUInteger index = (NSUInteger) indexPath.row;\n switch(section) {\n case SECTION_ONE:\n // to do something\n return cell;\n case SECTION_TWO:\n // to do something\n return cell;\n case SECTION_THREE:\n // to do something\n return cell;\n\n //...\n }\n\n return cell;\n}\n// ...\n\n```\n\nIt is cumbersome and hard maintenance in this way.\n\nWhile using `CBTableViewDataSource`:\n\n``` objective-c\n[_tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) {\n // section one\n [make makeSection:^(CBTableViewSectionMaker *section) {\n section.cell([OneCell class])\n .data(self.viewModel.oneDate)\n .adapter(^(OneCell * cell,id data,NSUInteger index){\n [cell configure:data];\n })\n .autoHeight();\n }];\n // section two\n [make makeSection:^(CBTableViewSectionMaker *section) {\n section.cell([TwoCell class])\n .data(self.viewModel.twoData)\n .adapter(^(FeedCell * cell,id data,NSUInteger index){\n [cell configure:data];\n })\n .autoHeight();\n }];\n\n // ... so on\n}];\n```\n\nIt has been concise and layered. Most important is that it make codes accord with the man's thought better.\n\n## Usage\n### Install\n\nusing `cocoapods`:\n\n``` ruby\npod 'CBTableViewDataSource'\n```\n\n### Import\n\n``` objective-c\n#import <CBTableViewDataSource/CBTableViewDataSource.h>\n```\n\n### Create `DataSource` and `Delegate`\n\n``` objective-c\n[_tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) {\n // section one\n [make makeSection:^(CBTableViewSectionMaker *section) {\n section.cell([OneCell class])\n .data(self.viewModel.oneDate)\n .adapter(^(OneCell * cell,id data,NSUInteger index){\n [cell configure:data];\n })\n .autoHeight();\n }];\n // section two\n [make makeSection:^(CBTableViewSectionMaker *section) {\n section.cell([TwoCell class])\n .data(self.viewModel.twoData)\n .adapter(^(FeedCell * cell,id data,NSUInteger index){\n [cell configure:data];\n })\n .autoHeight();\n }];\n\n // ... so on\n}];\n```\n\n## Example\n\n### Just using data\n\n``` objective-c\nUITableView tableView = [UITableView new];\n[tableView cb_makeSectionWithData:data];\n```\n\nIt will use default `UITalbeViewCell` as Cell Class in this way.\n\nThe data must follow convention as follows:\n\n1. data is a NSArray (NSArray < NSDictionary * >*).\n2. The key of dictionary as follows:\n - `text` use to set text for `UITableViewCell`'s textLabel\n - `detail` use to set text for `UITableViewCell`'s detailTextLabel\n - `value` use to set text for `UITableViewCell`'s detailTextLabel\n - `image` use to set image for `UITableViewCell`'s imageView\n - `accessoryType` use to set accessory type for `UITableViewCell`\n\n `value` and `detail` both be used to set text for `UITableViewCell`'s detailTextLabel. If use `detail` as key, the `detailTextLabel` will show at the bottom of `textLabel`. If use `value` as key, the `detailTextLabel` will show at the right of `textLabel`. Do not use both of them in the same time,and the first appear in array priority.\n\nFor example:\n\n``` objective-c\n_data = @[\n @{@\"text\":@\"Following\",@\"value\":@\"45\"},\n @{@\"text\":@\"Follower\",@\"value\":@\"10\"},\n @{@\"text\":@\"Star\",@\"value\":@\"234\"},\n @{@\"text\":@\"Setting\",@\"accessoryType\":@(UITableViewCellAccessoryDisclosureIndicator)},\n @{@\"text\":@\"Share\",@\"accessoryType\":@(UITableViewCellAccessoryDisclosureIndicator)}];\n```\n\nUI as follows:\n\n<img src = \"media/IMG_0238.png\" width = \"375\" style=\"margin:0 auto;\"/>\n\nCheck detail on file named `DemoTwoViewController.h` and `DemoTwoViewController.m`.\n\n### Using custom cell\n\n``` objective-c\n[tableView cb_makeSectionWithData:data andCellClass:[CustomCell class]];\n```\n\n`CustomCell` must provides a `Configuer:` method or `Configuer:index:` method to adapt data.\n\nFor example:\n\n``` objective-c\n- (void)configure:(NSDictionary *)row index:(NSNumber * )index {\n if (row[@\"avatar\"]) {\n [self.avatarView setImage:[UIImage imageNamed:row[@\"avatar\"]]];\n } else {\n [self.avatarView setImage:nil];\n }\n [self.nameLabel setText:row[@\"name\"]];\n [self.titleLabel setText:row[@\"title\"]];\n [self.detailLabel setText:row[@\"detail\"]];\n self.circleView.hidden = row[@\"unread\"] == nil;\n\n if([index intValue] &1) {\n self.contentView.backgroundColor = [UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];\n } else {\n self.contentView.backgroundColor = [UIColor whiteColor];\n }\n}\n```\nCheck detail on file named `CustomCell.h` and `CustomCell.m`\n\nUI as follows:\n\n<img src = \"media/IMG_0237.png\" width = \"375\" style=\"margin:0 auto;\"/>\n\nCheck detail on file named `DemoOneViewController.h` and `DemoOneViewController.m`.\n\n### More flexible setting\n\n``` objective-c\n[tableView cb_makeSection:^(CBTableViewSectionMaker * section) {\n\tsection.data(@[]);\n\tsection.cell([CustomCell class]);\n\tsection.adapter(^(CustomCell cell,id row,NSUInteger index) {\n\t\tcell.configure(row);\n\t});\n\tsection.event(^() {\n\t\t// do something\n\t})\n\t// other setting\n}];\n```\n\nHere show the case of single section.\n\n#### CBTableViewSectionMaker\n\n`CBTableViewSectionMaker` was used to setting some attribute for section.\nAvailable attribute as follows :\n\n##### data\n\nSetting the data be used show in `UITableView`,argument was required a NSArray.\n\nFor example:\n\n``` objective-c\nsection.data(@[@(goods1),@(goods2),...]);\n```\n\n##### cell\n\nSetting the `Cell Class` which was used to show data.\nThe identifier of cell will be register automatically.\n\nFor example:\n\n``` objective-c\nsection.cell([CustomCell class]);\n```\n\n##### adapter\n\nWas used to adapt cell and date.\n\nFor example:\n\n``` objection-c\nsection.adapter(^(CustomCell * cell,id row,NSUInteger index) {\n [cell configure:row];\n // ...\n});\n```\n\n##### event\n\nWas used to setting event when cell be touch, for example:\n\n``` objective-c\nsection.event(^(NSUInteger index,id row) {\n CustomViewController * controller = [CustomViewController new];\n controller.viewModel.data = row;\n [self.navigationController pushViewController:controller animated:YES];\n});\n```\n\n##### height\n\nUsed to setting height for `cell`. Is required a static value.\nThis height just vail for current section.\n\n``` objective-c\nsection.height(100);\n```\n\n##### autoHeight\n\nUsed to setting dynamic calculate height for cell.\n\n``` objective-c\nsection.autoHeight();\n```\n\nIf has setting `autoHeight`,the `height` will be invalid.\n\n##### headerTitle;\n\nUsed to setting header title for section. For example:\n\n``` objective-c\nsection.headerTitle(\"title\");\n```\n\n##### footerTitle;\n\nUsed to setting footer title for section. ditto.\n\n##### headerView;\n\nUsed to setting header view for section. For example\n\n``` objective-c\nsection.headerView(^(){\n UIView * headerView = [UIView alloc]initWithFrame:CGRectMake(0,0,320,40);\n // ...\n return headerView;\n})\n```\n\nIf has setting `headerView`,`headerTitle` will be invalid.\n\n##### footerView;\n\nUsed to setting footer view for section. ditto.\n\n### Multiple Section\n\n``` objective-c\n[tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) {\n\t[make headerView:^{\n\t\treturn [HeaderView new];\n\t}];\n\t[make makeSection: ^(CBTableViewSectionMaker * section) {\n\t\tsection.data(@[]);\n\t\tsection.cell();\n\t\tsection.adapter();\n\t\tsection.event();\n\t\t// ... so on\n\t}];\n\t[make makeSection: ^(CBTableViewSectionMaker * section) {\n\t\tsection.data(@[]);\n\t\tsection.cell();\n\t\tsection.adapter();\n\t\tsection.event();\n\t\t// ... so on\n\t}];\n\t[make makeSection: ^(CBTableViewSectionMaker * section) {\n\t\tsection.data(@[]);\n\t\tsection.cell();\n\t\tsection.adapter();\n\t\tsection.event();\n\t\t// ... so on\n\t}];\n\t// .. so on\n\t[make footView:^{\n\t\treturn [FooterView new];\n\t}];\n}]\n```\n\nUI as follows:\n\n\n<img src = \"media/IMG_0239.png\" width = \"375\" style=\"margin:0 auto;\"/>\n\n<img src = \"media/IMG_0240.png\" width = \"375\" style=\"margin:0 auto;\"/>\n\nCheck detail on file named `DemoThreeViewController.h` and `DemoThreeViewController.m`.\n\n#### CBTableViewDataSourceMaker\n`CBTableViewDataSourceMaker` was used to setting some attribute for `UITableView`.\nAvailable attribute as follows :\n\n##### makeSection\nUsed to add a section for `UITableView`.For example:\n\n``` objective-c\n[tableView cb_makeDataSource:^(CBTableViewDataSourceMaker * make) {\n\t[make makeSection: ^(CBTableViewSectionMaker * section) {\n\t // ...\n\t}\n}]\n```\n\n##### height\nUsed to setting default height for `UITableView`\n\n``` objective-c\nmake.height(100);\n```\n\nIf you had setting `height` or `autoHeight` for section, the `height` of here will invalid. Default is 40.\n\n##### headerView\nUsed to setting tableHeaderView for `UITableView`.Notice the difference between `tableHeaderView` and section‘s `headerView`.\n\nFor example:\n\n``` objective-c\nmake.headerView(^(){\n UIView * headerView = [[UIView alloc]init];\n // ...\n return headerView;\n});\n```\n\n##### footerView\nUsed to setting tableFooterView for `UITableView`. ditto.\n\n##### commitEditing\nUsed to setting `commitEditing` method for `UITableViewDelegate`.\n\n``` objective-c\n [make commitEditing:^(UITableView * tableView, UITableViewCellEditingStyle * editingStyle, NSIndexPath * indexPath) {\n // do somethings.\n}];\n```\n\n##### scrollViewDidScroll\nUsed to setting `scrollViewDidScroll` method for `UITableViewDelegate`\n\n````objective-c\n[make scrollViewDidScroll:^(UIScrollView * scrollView) {\n // do somethings\n}];\n````\n\n## Thinks\n\nThank you for using and supporting. Welcome to issue and pull request. I will deal with at first time.\n\nI refer to many masters in this framework. For example, I refer to famous `autolayout` framework `Masonary` when I design API. The way to dynamic calculate cell height is refer to `@forkingdog`'s `UITableView-FDTemplateLayoutCell`.\n\nThinks them for bring inspiration to me.\n\nContact me by email :460469837@qq.com",
"homepage": "https://github.com/cocbin/CBTableViewDataSource",
"license": "MIT",
"authors": {
"Cocbin": "460469837@qq.com"
},
"platforms": {
"ios": "8.0"
},
"source": {
"git": "https://github.com/cocbin/CBTableViewDataSource.git",
"tag": "1.1.0"
},
"source_files": "CBTableViewDataSource/*.{h,m}"
}