简体   繁体   中英

understanding methods in objective-c

for example we use this method in the tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 16;
}

i want to learn we don't call this method in anywhere but application reads this value how is it being? there are a lot of methods like this we did not call.

Your object has been set as the data source of the UITableView somewhere. Most likely, by making a connection in InterfaceBuilder, though it is straightforward to do so in code by setting the dataSource property of the UITableView:

- (void) setUpMyJunkMan
{
    myTableView.dataSource = self;
}

Once you have set your object as the data source, the table view will invoke the method as needed to determine what it needs to draw or how it needs to respond to events.

Your object is required to implement the UITableViewDataSource protocol (though, if you connected the data source via InterfaceBuilder, there may not be a complaint if you don't -- it is more of a compile time validation than a runtime one).

If you look at the declaration of UITableViewDataSource , you'll see that a number of methods are @optional . The rest are @required ; you must implement them to fulfill the contract of the protocol.

This is the key difference between data sources and delegates. Delegates can optionally implement any of the declared methods. Data sources create a much more formal relationship between the two objects wherein some of the methods must be implemented.

An easy way see why a method is being called - set a breakpoint, run in debug mode, and then look at the stack trace.

For this particular case - It's being called automatically by the framework when it renders the table view.

I think you really need to take a look at The Table View Programming Guide so that you have a good understanding of what methods you need to override (and not override) when using Table Views. If you are extending the TableViewController class the framework does a lot of the heavy lifting and you barely have to write any code.

numberOfSectionsInTableView: is being called by the table view.

You implement numberOfSectionsInTableView: as part of the UITableViewDataSource protocol. Each UITableView is given a dataSource . Normally, UITableView will be constructed by a UITableViewController which will set itself as the view's dataSource .

When the view is shown, it calls numberOfSectionsInTableView: on its dataSource .

This is explained in the Table View Programming Guide for iPhone OS .

This is part of a Delegate Interface.

At some point in your application (possibly in your UIBuilder) you have specified that the object that contains the method is actually the delegated object. This means that when you want to adjust the behaviour (in this case of a UITableView) you can without actually extending UITableView but mearly changing the delegate methods.

basically the UITableView class will look somehting like this.

- (void) AMethodinUiTableView 
{

int colums =[self.delegate numberOfSectionsInTableView:self];
}

for more info i would check out delgate programing and selectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM