views:

167

answers:

1

The formulation of the question may be confusing. I do understand why it is useful for a method like cellForRowAtIndexPath to receive a pointer the relevant UITableView.

What I don't understand is more Objective-C wise: I would like to put a name on is this special way of declaring methods?

Like if an object (e.g. UITableView) that have some internal protocol (e.g. UITableViewDataSource) would send messages to the implementers using a special way of referencing itself.

I.E. Instead of passing a reference to itself as a regular Objective-C message argument, the UITableView is using that special syntax?

+2  A: 

Edit

There's quite a long discussion in the comments to this answer, which refined the original question. I'll post the salient points here to help others who may have similar questions.

It boiled down to a confusion between two methods,

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

The first method is an instance method of UITableView which returns a UITableViewCell for a given index path. This method is called on a table view object.

The second method is a datasource method declared in the UITableViewDataSource protocol that asks for a UITableViewCell for a given index path.

The important difference is that the first is called on a table view instance by any class that may want a reference to a table cell, and the second is called by a table view instance on a data source class to ask for a cell to display at the given index path.

I hope this helps.


Most delegate/datasource protocols in Cocoa/Cocoa Touch follow this pattern. It's a way of saying something like "this table view wants this data".

Consider something like a text field object. You may have an interface with many text field objects that all report to the same delegate to define their behaviour for certain actions. By passing itself as an argument in a method to the delegate, the delegate knows which text field is calling the method.

I'm not sure I fully understand what you're asking, but this is the way objects inform their delegates/datasources which exact object is sending the method. In the case of UITableView it is less obvious because often there is only one table view per table view controller, but the text field example I gave should outline this fact.

Jasarien
Thanks but that is not answering my question. I do understand why this mechanism is useful. I just want to put a name on it, in order to understand why it is not done using the regular Objective-C way (i.e. passing yourself as a reference, as a regular message argument rather than a mysterious message prefix...)
Ariel Malka
I don't understand what you're asking. The table view instance calls this method on it's delegate/datasource and it *does* pass itself as a regular message argument. From the table view's perspective, the method looks like `[dataSource tableView:self cellForRowAtIndexPath:indexPath];` (probably simplified, but the point stands).
Jasarien
Sorry, but this is not what I call a regular message argument. "Regular" would mean: <code>[dataSource cellForRowAtIndexPath:indexPath withTableView:self]</code>. Anyway your example is helping to better understand this syntax.
Ariel Malka
There's no difference between `tableView:cellForRoatIndexPath:` and `cellForRowAtIndexPath:withTableView:` other than the order of the parameters, and the way the method reads, which, I guess is rather important, but anyway. As I said in my original answer, if you read the method as if it was a sentence, it would be similar to saying "this table view wants a cell for this indexPath" whereas your method is like saying "a cell for this indexPath is needed for this table view". I imagine it's down to personal taste to what you'd prefer, but there is no technical difference between the two.
Jasarien
Still, it be can confusing and I insist on the point that the subject is under-documented (I'm unable to find any Objective-C reference on the topic...) Take a look at the official UITableView docs: inside the "Instance Methods" paragraph, it mentions `- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath`. Now, how are you supposed to know that this method is also receiving a UITableView parameter?
Ariel Malka
It's not. It's a completely different method. `cellForRowAtIndexPath:` is an instance method of `UITableView` which returns a particular cell at a particular indexpath. However, `tableView:cellForRowAtIndexPath:` is a data source method defined in the `UITableViewDataSource` protocol that asks a data source for a cell that should be displayed at that indexpath.
Jasarien
Thanks, the whole discussion really helped me to dissipate the confusion (Objective-C digestion ache I suppose...)
Ariel Malka
No problem, happy I could help :)
Jasarien