Reputation: 46823
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
I'm pretty comfortable with obj-c, but I don't understand this method signature. Specifically why this method has all that extra stuff before the method name, and what it means. Like I get that the -
is an instance method, and that the return type is NSInteger
but why is tableView: (UITableView *) tableView
in front of the method name?
WHy do some instance methods for the UITableViewDataSource protocol have nothing infront of the name? numberOfSectionsInTableView
is defined differently.
Can someone explain it to me?
Upvotes: 0
Views: 629
Reputation: 185681
Lets break it down into parts:
-
This means it's an instance method. The alternative is +
which means class method.
(NSInteger)
This is the return type of the method. In this case it's NSInteger
.
tableView:
The first component of the selector name (which is, in full, tableView:numberOfRowsInSection:
). The :
indicates that a parameter follows.
(UITableView *)
The type of the parameter.
tableView
The name of the parameter. This is largely irrelevant in the method signature (except as a hint to the reader as to the purpose), but in the implementation this is the variable that is bound to that parameter.
numberOfRowsInSection:
The next component of the selector name.
(NSInteger)
The type of the second parameter.
section
The name of the second parameter.
Note, the only required space in this entire line is the one between tableView
and numberOfRowsInSection:
. All other spaces can be elided to produce
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
Though the most common format you'll find looks like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Edit: Looks like there's still some confusion about the latter part of the question. The tableView:
component of the selector is there to provide the UITableView*
instance that's asking the question. All of the methods in the UITableViewDataSource
protocol provide the sending tableview as an argument. Some of these methods have other arguments, and some don't. The ones that have additional arguments are all formatted as tableView:someOtherThing:
(e.g. tableView:numberOfRowsInSection:
), but this isn't required. It could be called numberOfRowsInTableView:forSection:
, or numberOfRowsInSection:ofTableView:
, or even foo:bar:
, but it's a stylistic choice that was made by the API developer to preserve a consistent naming scheme that assist both the developer and the person reading the code later. As for the methods that don't take any other parameters, they look like numberOfSectionsInTableView:
because that's just a natural name for the method. They can't be called tableView:numberOfSections
because that's an illegal selector (all components after the first must have a parameter associated, and therefore must have a trailing :
).
Upvotes: 9
Reputation: 38728
In each case a reference to the tableView
that is asking for data or calling a delegate method is given to the user
// |
// V
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// |
// V
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
That way if you have one datasource
/delegate
object that is in charge of multiple UITableView
's you can distinguished between which one is asking for data/ is calling a method.
This is a pretty common pattern.
If you are using a UITableViewController
the likelihood is that you will ignore the tableView
parameter anyway as you are normally only dealing with the one UITableView
The reason for the ordering of the params e.g. tableView
being at the beginning is probably preference and it reads better when there are multiple parameters, however when you only have the tableView
paramater is must be the last.
Upvotes: 2