views:

568

answers:

4

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.

+1  A: 

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.

bpapa
Setting a breakpoint won't actually help to understand why the method is being called, it'll just confirm what called it...When implementing the UITableViewDataSource protocol, there is no overriding going on. Just implementation (save for when leveraging the controller as you mentioned).
bbum
A: 

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.

Will Harris
Again, it is not a delegate. The UITableViewDataSource protocol has very explicit required methods to meet the requirements.
bbum
@bbum, I don't think delegate protocols are required to have only optional methods. See SKProductsRequestDelegate and ABNewPersonViewControllerDelegate for examples having required methods.
Will Harris
Both bugs, unfortunately.
bbum
The Cocoa Fundamentals Guide says (http://tinyurl.com/howdelegationworks) 'If the delegating class declares a formal protocol, the delegate may choose to implement those methods marked optional, but it must implement the required ones.' I take that to mean that delegate protocols can have required methods. The same doc also says 'A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data.' You're right that dataSource is not a delegate and I've edited the answer to reflect this.
Will Harris
A: 

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.

Bluephlame
This answer is incorrect.(1) This is not delegation.(2) The *example* syntax for the method is just plain wrong.The suggestion to check out delegate programming and selectors is sound; they are related.
bbum
+10  A: 

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.

bbum
dataSoure -> dataSource
Wil Shipley