Commandtabel_ Proxy method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.