Today, the teacher took us again to review the block, the application of the block, memory management and block syntax, before learning the feeling is difficult to listen to the Mengmengdongdong, today, the teacher with a review once again, feel and understand a lot, Here I would like to share my personal understanding of the point of knowledge today.
Commandtabel can be implemented by proxy method, can also use block, first talk about the agent bar
Proxy is actually a list of methods that let you implement the necessary methods and optional methods
1. Use Cmdtabel to write an agent yourself and write the method inside
1) Create a class that inherits from NSObject and is named Cmdtabel;
2). h declares Proxy properties
|| The name of the agent (note: Look at any one of the delegate can be observed @protocol agent <NSObject>)
@protocol Cmdtabledatasource <NSObject>
Set the proxy method, where the proxy method can be set to specify who is the one who is chosen to implement the
@required
Observing other agents you will find that the method of the system will indicate whose agent---cmdtable: (cmdtabel*) Table
Number of rows returned
-(Nsinteger) Cmdtablenumberofrows: (cmdtabel*) Table;
Returns the data for a row
-(NSString *) cmdtable: (cmdtabel*) Table Textforrowatindex: (Nsinteger) row;
@end
3) proxy for setting up data
Signed a proxy method named Cmdtabledatasource, similar to the
/**
* @interface viewcontroller:uiviewcontroller<uipickerviewdatasource>
**/
@property (nonatomic,weak) id<cmdtabledatasource>dataSource;
4) Open a class method for easy external invocation
-(void) Reloadtabel;
5). m file
The role of the Implementation method
Update table
-(void) Reloadtabel {
Get row Count
Nsinteger number = [_datasource cmdtablenumberofrows:self];
for (int i = 0; I <number; i++) {
NSLog (@ "%@", [_datasource cmdtable:self textforrowatindex:i]);
}
}
2. Create a class that inherits from NSObject and name it controller.
1) Sign the agency agreement
. h
@interface controller:nsobject<Cmdtabledatasource>
{
An array of stored data
nsarray* dataList;
Data Display Container
Cmdtabel *tableview;
}
Open instance method for easy external invocation
-(void) viewdidload;
3. Implementing Proxy Methods
#pragma the proxy method for Mark Cmd
Number of rows returned
-(Nsinteger) Cmdtablenumberofrows: (cmdtabel*) Table {
return Datalist.count;
}
Returns the data for a row
-(NSString *) cmdtable: (cmdtabel*) Table Textforrowatindex: (Nsinteger) row{
return [dataList Objectatindex:row];
}
4. Display data
-(void) viewdidload{
DataList = @[@ "First page", @ "first page", @ "first page", @ "first page", @ "first page", @ "first page", @ "first page";
TableView = [[Cmdtabel alloc]init];
Tableview.deledate = self;
// ||
TableView. DataSource = self;
[TableView Reloadtabel];
}
At the beginning of the use of the time do not understand the agent is how to write their own, behind the signing of the agent when also do not know that the signature is the name of the agent. Actually, there is one sentence
Tableview.datasource = self; is equivalent to
Tableview.deledate = self;
。
When you write the proxy method to refer to the system proxy method, who's proxy method to indicate clear
Example: (TableView of the system)
-(CGFloat)TableView: (UITableView *) TableView heightforrowatindexpath: (Nsindexpath *) Indexpath;
We have to pay attention when we write ourselves.
-(NSString *)cmdtable: (cmdtabel*) Table Textforrowatindex: (nsinteger) row;
OK today first write here, I want to continue to study, do not know if you can understand, if not very clear message to tell me, hope can help everyone.
-----< God darling >
If you are hungry for knowledge, be foolish in modesty.
Commandtabel_ Proxy methods