Before looking at Objc.io #1 light View controllers See a good tip: separating the data source from the Uitableviewcontroller, which reduces the size of the Uitableviewcontroller, It also allows the program to have a better architecture.
Since Uitableviewcontroller is one of the most frequently used view controllers in iOS, take notes here and record this technique.
The first is the storyboard (of course you can also use the combination of code + XIB):
Create a new cell class, and connect the outlets in the storyboard with the following code:
#import <UIKit/UIKit.h> @interface cell:uitableviewcell-(void) Configurefordata: (NSString *) data; @end
#import "Cell.h" @interface Cell () @property (weak, nonatomic) Iboutlet UILabel *datatitlelabel; @property (Weak, nonatomic) Iboutlet UIButton *datadetaillabel; @end @implementation cell-(void) Configurefordata: (NSString *) Data { self.dataTitleLabel.text = data; [Self.datadetaillabel settitle:@ "1" forstate:uicontrolstatenormal];} -(ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{self = [super Initwithstyle:style Reuseidentifier:reuseidentifier]; if (self) { //initialization code } return self;} -(void) awakefromnib{ //initialization code}-(void) setselected: (BOOL) selected animated: (BOOL) animated{ [ Super setselected:selected animated:animated]; Configure The view for the selected state} @end
the Configurefordata method in the cell class is used to configure the contents of the UI in the cell.
Back to the Tableviewcontroller class, the code is as follows:
#import "TableViewController.h" #import "DataSource.h" #import "Cell.h" @interface Tableviewcontroller () @property ( Strong, Nonatomic) Nsarray *array, @property (Strong, nonatomic) DataSource *datasource; @end @implementation tableviewcontroller-(void) viewdidload {[Super viewdidload]; Self.array = @[@ "1", @ "2", @ "3", @ "1", @ "2", @ "3", @ "1", @ "2", @ "3"]; [Self setuptableview];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* Set the data source for the table and registernib */-(void) Setuptableview {tableviewcellconfigureblock Configurecell = ^ (Cell *cell, Nsstr ing *str) {[Cell configurefordata:str]; }; Self.datasource = [[DataSource alloc] Initwithitems:_array cellide ntifier:@ "Cell" Configurecellblock:configurecell]; Self.tableView.dataSource = Self.datasource;} #pragma mark-uitableviewdelegatE (cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return 100.0;} -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {NSLog (@ "%@", Self.array [Indexpath.row]);} @end
The Setuptableview method implements the Uitableviewdatasource "outsourcing" of the table to the Tabledatasource class.
This class implements Uitableviewdelegate, including the behavior of clicking on a row in a table, the cell's height, and so on.
Finally look at the Tableviewdatasource class that assumes responsibility for tabular data sources:
#import <foundation/foundation.h>typedef Void (^tableviewcellconfigureblock) (ID cell, ID item); @interface Datasource:nsobject <uitableviewdatasource>-(ID) Initwithitems: (Nsarray *) Anitems cellIdentifier: ( NSString *) Acellidentifier Configurecellblock: (tableviewcellconfigureblock) aconfigurecellblock;-(ID) Itematindexpath: (Nsindexpath *) Indexpath; @end
first the class must obey the Uitableviewdatasource delegate and then define a block type that configures the cell.
The implementation code for this class is as follows:
#import "DataSource.h" @interface DataSource () @property (nonatomic, strong) Nsarray *items; @property (nonatomic, copy) NSString *cellidentifier; @property (nonatomic, copy) Tableviewcellconfigureblock configurecellblock;@ End@implementation datasource#pragma mark-initialization-(ID) init {//Only through InitWithItems:cellIdentifier:configureCe Llblock: Method initializes return nil;} -(ID) Initwithitems: (Nsarray *) Anitems cellidentifier: (NSString *) Acellidentifier Configurecellblock: ( Tableviewcellconfigureblock) aconfigurecellblock{self = [super init]; if (self) {self.items = Anitems; Self.cellidentifier = Acellidentifier; Self.configurecellblock = [Aconfigurecellblock copy]; } return self;} -(ID) Itematindexpath: (Nsindexpath *) Indexpath {return self.items[(Nsuinteger) indexpath.row];} #pragma mark uitableviewdatasource/* Required methods */-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: (nsinteger) Section {return self.items.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:self.cellIdentifier Forindexpath:indexpath]; ID item = [self Itematindexpath:indexpath]; /** the Configurecellblock as a property, so that the class can be reused as long as the Tableviewcontroller custom code block and passed as a parameter can be reused key: Do not be specifically implemented code intrusion, Only need to call the interface and give the interface can be */self.configurecellblock (cell, item); return cell;} @end
Next Cellforrowatindexpath method of Self.configurecellblock (cell, item);
The function of this code is undoubtedly to configure the contents of the cell, generally by the user-defined cell class self-implementation, there is no implementation details involved, so that the Tableviewdatasource class can be well reused.
Operation Result:
By the way a Demo come up, interested can be downloaded to see.
Reference: Lighter View Controllers