在一個IOS項目中,viewcontroller一般是最大的文件,而且包含了許多沒必要要的代碼,重用率是最低的。sublime-text
咱們能夠嘗試給viewcontroller瘦身,讓他看起來不是那麼的臃腫。數組
今天說到的是,UITableViewDataSource。框架
UITableview多是平時寫項目中用到的最多的組件了,使用它要實現它的代理協議和數據源方法,每次都是那麼些東西方法controller中,看起來不舒服,咱們能夠給他瘦身一下。好比這樣作。ide
由於datasource基本上是圍繞數組去作一些事,更針對的說就是圍繞viewcontroller的數據數組做一些事情,咱們能夠把它抽出來,單獨的放在一個類中,咱們使用一個block來設置cell,也能夠使用delegate來作這件事,這徹底取決於你。工具
下面上代碼,舉個栗子~atom
1 // 2 // ArrayDataSource.h 3 // LrdBasicFramework 4 // 5 // Created by 鍵盤上的舞者 on 5/21/16. 6 // Copyright © 2016 鍵盤上的舞者. All rights reserved. 7 // 封裝的一個dataSource類,能夠使controller更加的輕量級 8 9 #import <Foundation/Foundation.h> 10 11 typedef void(^TableViewCellConfigureBlock)(id cell, id item); 12 13 @interface ArrayDataSource : NSObject <UITableViewDataSource> 14 15 - (id)initWithItems:(NSArray *)items 16 cellIdentifier:(NSString *)identifier 17 configureBlock:(TableViewCellConfigureBlock)configureBlock; 18 19 - (id)itemAtIndexPath:(NSIndexPath *)indexPath; 20 21 @end
1 // 2 // ArrayDataSource.m 3 // LrdBasicFramework 4 // 5 // Created by 鍵盤上的舞者 on 5/21/16. 6 // Copyright © 2016 鍵盤上的舞者. All rights reserved. 7 // 8 9 #import "ArrayDataSource.h" 10 11 @interface ArrayDataSource () 12 13 @property (nonatomic, strong) NSArray *items; 14 @property (nonatomic, copy) NSString *identifier; 15 @property (nonatomic, copy) TableViewCellConfigureBlock configureBlock; 16 17 @end 18 19 @implementation ArrayDataSource 20 21 - (instancetype)init { 22 return nil; 23 } 24 25 - (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)identifier configureBlock:(TableViewCellConfigureBlock)configureBlock { 26 self = [super init]; 27 if (self) { 28 self.items = items; 29 self.identifier = identifier; 30 self.configureBlock = configureBlock; 31 } 32 return self; 33 } 34 35 - (id)itemAtIndexPath:(NSIndexPath *)indexPath { 36 return self.items[(NSUInteger) indexPath.row]; 37 } 38 39 #pragma mark - DataSource 40 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 41 return self.items.count; 42 } 43 44 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 45 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier forIndexPath:indexPath]; 46 id item = [self itemAtIndexPath:indexPath]; 47 self.configureBlock(cell, item); 48 return cell; 49 } 50 @end
而後咱們能夠在viewcontroller中來這樣使用它spa
1 // 2 // DemoRootController.m 3 // LrdBasicFramework 4 // 5 // Created by 鍵盤上的舞者 on 5/21/16. 6 // Copyright © 2016 鍵盤上的舞者. All rights reserved. 7 // 8 9 #import "DemoRootController.h" 10 #import "ArrayDataSource.h" 11 #import "DemoFrameController.h" 12 #import "DemoFouctionController.h" 13 #import "DemoDIYController.h" 14 #import "DemoCommUtils.h" 15 16 static NSString *identifier = @"menu_cell"; 17 18 @interface DemoRootController () 19 20 @property (nonatomic, strong) ArrayDataSource *arrayDataSource; 21 22 @end 23 24 @implementation DemoRootController 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 self.title = @"Demo"; 29 //初始化 30 [self initUI]; 31 } 32 33 - (void)initUI { 34 NSArray *items = @[@"自定義控件", @"功能性拓展", @"經常使用工具類", @"基層框架"]; 35 self.arrayDataSource = [[ArrayDataSource alloc] initWithItems:items cellIdentifier:identifier configureBlock:^(UITableViewCell *cell, NSString *item) { 36 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 37 cell.textLabel.text = item; 38 }]; 39 self.tableView.dataSource = self.arrayDataSource; 40 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier]; 41 } 42 43 #pragma mark - delegate 44 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 45 if (indexPath.row == 0) { 46 //自定義控件 47 DemoDIYController *vc = [[DemoDIYController alloc] initWithStyle:UITableViewStyleGrouped]; 48 vc.title = [self.arrayDataSource itemAtIndexPath:indexPath]; 49 [self.navigationController pushViewController:vc animated:YES]; 50 }else if (indexPath.row == 1) { 51 //功能性拓展 52 DemoFouctionController *vc = [[DemoFouctionController alloc] initWithStyle:UITableViewStyleGrouped]; 53 vc.title = [self.arrayDataSource itemAtIndexPath:indexPath]; 54 [self.navigationController pushViewController:vc animated:YES]; 55 }else if (indexPath.row == 2) { 56 //經常使用工具類 57 DemoCommUtils *vc = [[DemoCommUtils alloc] initWithStyle:UITableViewStyleGrouped]; 58 vc.title = [self.arrayDataSource itemAtIndexPath:indexPath]; 59 [self.navigationController pushViewController:vc animated:YES]; 60 }else if (indexPath.row == 3) { 61 //基層框架 62 DemoFrameController *vc = [[DemoFrameController alloc] initWithStyle:UITableViewStyleGrouped]; 63 vc.title = [self.arrayDataSource itemAtIndexPath:indexPath]; 64 [self.navigationController pushViewController:vc animated:YES]; 65 } 66 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 67 } 68 69 @end
是否是看起來viewcontroller瘦了許多??.net
轉自:http://www.lrdup.net/archives/1046代理