Code optimization – leaner tableView, pulling away from tableView DataSource Delegate

Title: code optimization - leaner tableView, tableView DataSource, Delegate, tags:, [iOS, APP, development]


UITableView is undoubtedly our development in iOS applications using a very high frequency control, the use of methods, no less than the creation of objects, associated delegate, datasource, to achieve the corresponding method

Almost every time we use UITableView, we call several fixed methods, such as

/ / returns the number of - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: section (NSInteger); / / return height - (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath * indexPath); / / return the cell instance object (UITableViewCell *) tableView: (* UITableView) tableView cellForRowAtIndexPath: (NSIndexPath * indexPath) //cell click event - (void) tableView: (UITableView * tableView) didSelectRowAtIndexPath: (NSIndexPath * indexPath)

This is undoubtedly a lot of repetitive code, is there a way to simplify the use of UITableView, to avoid these repetitive nature of the code, and compatible with the majority of the use of UITableView?

UITableView Deleaget, DataSource pulled out is undoubtedly a good way to achieve a method of implementation of general agent in charge of the class, through the block to reduce the amount of code correction, VC.
< –more–>

Code instance

Generic processing class CustomDeleagte.h

#import < Foundation/Foundation.h> #import; < UIKit/UIKit.h> typedef void (^TableViewCellConfigureBlock) (NSIndexPath* indexPath, ID cell, ID item); typedef void (^TableViewCellDidSelectBlock) (NSIndexPath* indexPath, ID cell, ID item); typedef CGFloat (^TableViewCellHeightBlock) (NSIndexPath* indexPath, ID item); @interface CustomDeleagte: NSObject < UITableViewDataSource, UITableViewDelegate> - (ID) initWithItems: (NSArray * aItems) cellIdentifier: (NSString *) aIdentifier configureCellBlock: (TableViewCellConfigureBlock) aConfigureCellBlock cellHeightBlock: (TableViewCellHeightBlock) aHeightBlock didSelectBlock: (TableViewCellDidSelectBlock) aDidSelectBlock; (void) - handleTableViewDataSourceAndDelegate: (UITableView*) ATableView; - - (ID) itemAtIndexPath: (NSIndexPath *) indexPath; @property (nonatomic, copy) NSArray, *items; @end

1.TableViewCellConfigureBlock is used to pass in the data source and load the data
2.TableViewCellDidSelectBlock callback. Click the event
3.TableViewCellHeightBlock to calculate the cell height

The three block basically covers the general use of UITableView

Generic processing class implementation CustomDeleagte.m

#import "CustomDeleagte.h" #import "UITableViewCell+Extension.h" @interface (CustomDeleagte) @property (nonatomic, copy) NSString* cellIdentigier @property (nonatomic, copy); TableViewCellConfigureBlock configureCellBlock; @ property (nonatomic, copy) TableViewCellDidSelectBlock didSelectBlock @property (nonatomic, copy); TableViewCellHeightBlock heightBlock; @end @implementation CustomDeleagte (ID) initWithItems: (NSArray * cellIdentifier: (aItems) NSString *) aIdentifier configureCellBlock: (TableViewCellConfigureBlock) aConfigureCellBlock cellHeightBlock: (TableViewCellHeightBlock) aHeightBlock didSelectBlock: (TableViewCellDidSelectBlock) aDidSelectBlock{self = [super init]; if (self) {self.items = aItems; Self.cellIdentigier = aIdentifier; self.configureCellBlock = aConfigureCellBlock; self.heightBlock = aHeightBlock; self.didSelectBlock = aDidSelectBlock;} return self;} - (ID) itemAtIndexPath: (NSIndexPath*) indexPath{return self.items[(NSUInteger) indexPath.row];} - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section{return self.items.count;} - (UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath{UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentigier]; if (cell!) {[UITableViewCell registerTabelView:tableView nibIdentifier:self.cellIdentigier]; cell = [ta BleView dequeueReusableCellWithIdentifier:self.cellIdentigier] ID item;} = [self itemAtIndexPath:indexPath]; self.configureCellBlock (indexPath, cell, item); return cell;} - (void) tableView: (UITableView * tableView) didSelectRowAtIndexPath: (NSIndexPath *) indexPath{ID item = [self itemAtIndexPath:indexPath]; ID cell = [tableView cellForRowAtIndexPath:indexPath]; self.didSelectBlock (indexPath, cell, item);} / / delegate datasource, (void) - handleTableViewDataSourceAndDelegate: (UITableView * aTableView{) aTableView.delegate = self; aTableView.dataSource = self;} - (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath *) indexPath{ID item = [self itemAtIndexPath:indexPath]; Return self.heightBlock (indexPath, item);} @end

Extending several methods to UITableViewCell


#import < UIKit/UIKit.h> @interface; UITableViewCell (Extension) / cell (void) registerTabelView: registered + (UITableView*) aTable nibIdentifier: identifier (NSString*); / / load data - (void) configure: (UITableViewCell*) aCell custimObj: (ID) obj indexPath: indexPath (NSIndexPath*); / / cell height calculation according to the data source 44.0f + CGFloat (the default return getCellHeightWitCustomObj:) (ID) obj indexPath: (NSIndexPath*) indexPath; @end


#import "UITableViewCell+Extension.h" @implementation UITableViewCell (Extension) / cell (void) registerTabelView: registered + (UITableView*) aTable nibIdentifier: (NSString*) identifier{[aTable registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];} / / load data - (void) configure: (UITableViewCell*) aCell custimObj: (ID) obj indexPath: (NSIndexPath*) indexPath{} / / calculation according to the data source cell default height return 44.0f + (CGFloat) getCellHeightWitCustomObj: (ID) obj indexPath: (NSIndexPath*) indexPath{return 44.0f @end;}

Override the two methods in the custom cell class,
configure for data loading, and
getCellHeightWitCustomObj for computing height

The registerTabelView method is used in CusetDelegate to register cell to the corresponding tableView

Method of use in code [[NSArray = alloc]init]; = [[UITableView alloc]initWithFrame:CGRectMake (0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain]; TableViewCellConfigureBlock = ConfigBlock ^ (NSIndexPath* indexPath, WelfareRuleCell* cell, ID item) {[cell configure:cell}; custimObj:item indexPath:indexPath]; TableViewCellDidSelectBlock = DidBlock ^ (NSIndexPath* indexPath, ID cell, ID item) {}; TableViewCellHeightBlock heightBlock = ^CGFloat (NSIndexPath* indexPath, ID item) {return [WelfareRuleCell}; getCellHeightWitCustomObj:item indexPath:indexPath]; self.customDelegate = [[CustomDeleagte alloc]initWithItems:_data cellIdentifier:@ Welfa " ReRuleCell "configureCellBlock:ConfigBlock cellHeightBlock:heightBlock didSelectBlock:DidBlock] [self.customDelegate]; [self.view];

Custom cell class

#import "WelfareRuleCell.h" @implementation WelfareRuleCell - awakeFromNib [super awakeFromNib] (void) {code}; / / Initialization - (void) configure: (UITableViewCell *) aCell custimObj: (ID) obj indexPath: (NSIndexPath * indexPath{) / / data loading self.XXX = XXX;} + (CGFloat) getCellHeightWitCustomObj: (ID) obj indexPath: (NSIndexPath * indexPath{) / / height calculation code return XXX @end;}


Using the Delegate and DataSource methods to use tableView, the code in the controller can be greatly simplified and the code logic can be clearer