0

What does the following method declaration describe?

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

What I don't understand is the description of the result of the method:

(UITableViewCell *)tableView:(UITableView *)tableView 

It should return only one result, but apparently it is returning two. How?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
grenzfrequence
  • 291
  • 3
  • 11
  • Many thanks for this quick answer. Now i understand it. For me objective c is more complicated to read than C++: to split a method name is confusing for me. But this is up to me. Perhaps other people like this syntax. – grenzfrequence May 13 '14 at 11:37
  • http://stackoverflow.com/questions/8079471/how-does-cellforrowatindexpath-work/8079761#8079761 – Omarj May 13 '14 at 11:39

2 Answers2

3

There is actually only one return value (result).

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

in C++/Java would be something like this

UITableViewCell * getTableViewCellForRowAtIndexPath(UITableView *tableView, NSIndexPath *indexPath)

The whole method name in Obj-C is -tableView:cellForRowAtIndexPath:. Obj-C decided to have method names similar to sentences. The parameters are like words in that sentence. Also note we don't usually use get in the beginning of the method name.

In this case, the confusion is understandable because the method name cannot be read as a sentence.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

That is OK if you are beginner. See, you can never eat a pizza in one byte, you break it down into small pieces so that you can eat it easily.

The same fact has been realized by Objective-C.

Consider a simple case:

Say we define a method in regular C that takes 5 parameters, and it adds the first two, the result of first and second argument is multiplied with the third argument, whose result is divided by the 4th argument, and at the end, previous result is added into fifth argument.

The following would be the definition of the function:

float aLittleComplexFunction(int first, int second, int third, int fourth, int fifth) {
    return ((((float)first + second) * third ) / fourth ) + fifth;
}

Now, note that I couldn't come up with a good function name because it was tough. A function name like addTwoNumbersAndThenMultiplyAndThenDivideAndThenAdd would be overkill.

Now, when it comes to Objective-C, it breaks down into small pieces as following:

float addNumber:(int)first into:(int)second multiplyWith:(int)third divideWith:(int)fourth addWith:(int)fifth {
    return ((((float)first + second) * third ) / fourth ) + fifth;    
}

Same is the case with what you have asked. Remember in Objective-C, function name is split, and each part comes with it's relevant parameter, overall making it easier to get to know the actual purpose of the function.

You might feel it difficult to get started with it, but once you do, you'll hate the typical C way.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76