要说 iOS 开发中有什么是最离不开的组件,我想是 UITableViewController。几乎每一个iPhone 应用都会有它的身影,用了它,,不用考虑内容过多的问题,,熟悉自定义 UITableViewCell 以后,用起来几乎和 UITableViewController 没啥差。git
基本上,我认为全部能用 UITableViewController 实现的页面都该是UITableViewController,使用它的好处有:github
而后在使用在使用这些好处的时候,很容易就会发生 Controller 承载太多的功能:浏览器
……简直包山包海。网络
而后就有了 整洁的 Table View 代码、更轻量的ViewControllers 这种最佳实践文章。布局
这篇文章的观点是把 DataSource 分离出来,独立成一个类,而后将 tableView 的 dataSource 属性指给其实例,如:ui
- (void)viewDidLoad { self.tableView.dataSource = [[MenuDataSouirce alloc] initWithItems:@[@"用户", @"问题", @"文章", @"标签", @"登陆"]]; .... }
接下来就是在 dataSource 中建立Cell、设置样式、填充数据,在 Controller 中实现了 delegate,一切仿佛都变的好了简洁了又能够爱下去了。atom
随着项目的进行,会发现有不少「无趣」的页面都要作重复的工做,如侧边栏、用户设置、用户登陆页这种,每次都要去实现一遍 dataSource 里面的这些方法:spa
tableView:cellForRowAtIndexPath: tableView:numberOfRowsInSection: tableView:numberOfSection:
WTF!!你发现这些堆代码的工做消耗了不少时间,而这些无聊工做应该更快更好的完成。code
若是你遇到相同的苦恼,SSDataSources 或许能把你解救出来。blog
无论哪种类型,SSDataSources 都支持动态增长元素、删除元素。此外它也同时适用于 UICollectionView。还有一些对 FooterView、HeaderView 的冗余属性,总之让你和 tableView 打交道的时候更舒心。
下面贴一下使用 SSDataSource 实现的侧边栏(代码还不到 50 行):
#import "MenuController.h" #import "SSArrayDataSource.h" #define TOP_CELL_HEIGHT 64.0f @interface MenuController () @property (nonatomic, strong) SSArrayDataSource *dataSource; @property (nonatomic, strong) NSArray *items; @end @implementation MenuController { } - (void)viewDidLoad { [super viewDidLoad]; self.items = @[ @"用户", @"问题", @"文章", @"标签", @"", @"登陆"]; self.dataSource = (id)[[SSArrayDataSource alloc] initWithItems:self.items]; [self.dataSource setCellConfigureBlock:^(UITableViewCell *cell, NSString *s, id parentView, NSIndexPath *indexPath) { cell.textLabel.text = s; }]; self.tableView.dataSource = self.dataSource; } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat result = 44; if(indexPath.row == 0) { result = TOP_CELL_HEIGHT; } else if([self.items[(NSUInteger) indexPath.row] isEqualToString:@""]) { CGFloat screen_height = self.view.bounds.size.height; CGFloat other_hight_sum = TOP_CELL_HEIGHT + (self.items.count-2) * 44.f; result = MAX(screen_height - other_hight_sum, 0.f); } return result; } @end
效果图:
什么?心动了?赶快打开浏览器去看看吧:https://github.com/splinesoft/SSDataSources