-4

I am starting with ObjectiveC at the moment and wondering what this function definition means

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    // access to tableView, is it a variable? 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    ...
    return cell;
}

I am wondering what is tableView and what does the part before the function name cellForRowAtIndexPath mean.

As i see from the example code, tableViewis a variable. But what does the (UITableViewCell *)tableView:(UITableView *)tableViewmean? I thought the return value is only before the function name?

Update 1 Maybe I should mention i am familiar with languages like c/c++, java, ...

Update 2 I am not interested in the meaning of the function or what its supposed to do, just the syntax and the definition of any function like that

Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57
  • 2
    You mean: -(returnType)methodNameUsuallyIntroducingNextParameter:(ParameterType)firstParameter followingMethodNameUsuallyIntroductionNextParameter:(ParameterType)secondParameter etc. Using what other programming language could we help you to understand? – Larme Jan 08 '16 at 14:16
  • `tableView` is simply part of the function name (and it introduces the first parameter, which is the `UITableView` instance for which the delegate needs to supply the cell). – Tim Vermeulen Jan 08 '16 at 14:32
  • okay, so parameter definition and the function name is mixep up.. got that... now that pure function name is ```tableView:cellForRowAtIndexPath```right? Now how would i call that function manually – Martin Mlostek Jan 08 '16 at 14:34
  • any idea why this got downvoted? what did i wrong? – Martin Mlostek Jan 08 '16 at 15:10
  • The down votes are because there are countless existing Objective-C tutorials and books and documentation that explain the syntax, not to mention existing questions here that already answer this question. – rmaddy Jan 08 '16 at 15:24
  • woops. sorry, somehow i was relying on the suggestion while i was typing the question instead of searching stackoverflow. shall i delete this duplicate? – Martin Mlostek Jan 08 '16 at 15:38

4 Answers4

3

ObjectiveC has a very interesting naming convention.

The - at the beginning means that this is an instance method. A class (static) method has a +.

Next comes the return type (UITableViewCell *). The little * indicates that the return value is a proper object and not a primitive data type like NSInteger.

This is followed by the parameters. Actually the parameters and the method name are kind of mixed up. If you remove the formal parameters you get something like

tableView:cellForRowAtIndexPath:

Which is the name of the function. You don't pass your parameters at the end like in other languages, but right behind the colon. Thus the method names are very descriptive.

A Java pendant might be looking like this

public UITableViewCell getCell(UITableView view, NSIndexPath path)

(ObjectiveC does not have public/private, therefore I made the Java method public).

Marc
  • 6,051
  • 5
  • 26
  • 56
  • okay, but where do you get the ```getCell```from in your java example? How would i call this function manually in my code? – Martin Mlostek Jan 08 '16 at 14:28
  • You'd need two objects of course, the UITableView (lets call it `view`) and the NSIndexPath (lets call it `path`). In Java it would be `getCell(view, path)`. In ObjC it would be `[self tableView:view cellForRowAtIndexPath:path]`. **But** in this specific example you don't need to call it because it's a delegate method! – Marc Jan 08 '16 at 14:47
  • okay, so basically there is no unique function name (like in other languages like java, c++) its more a combination of the parameter names instead? – Martin Mlostek Jan 08 '16 at 14:56
  • Mh kinda.. as I said in my answer, if you strip the unnecessary stuff from the methods *signature*, you will get something that sounds like a method *name*. – Marc Jan 08 '16 at 15:01
  • oh yes.. parameter names are behind the XXX:(TYPE9) thing.. and the XXX combined are the "function-name" thing :) thanks! – Martin Mlostek Jan 08 '16 at 15:08
  • @martynmlostekk Objective-C does not have named parameters. It has parameters interleaved with the method name. The method name `tableView:cellForRowAtIndexPath:` is distinctly different from `tableView:cellForRowAtIndexPath:reverseOrder:`, for example. – bbum Jan 08 '16 at 23:49
2

- (UITableViewCell *) is the return type. You're going to need to return a UITableViewCell or descendant.

tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is the method name. It contains two parts:

  • tableView:(UITableView *)tableView, the first argument; and
  • cellForRowAtIndexPath:(NSIndexPath *)indexPath, the second argument.

This particular method is part of a delegate protocol — one of the design patterns Apple (amongst others) uses, in which a particular class is responsible for a group of things but delegates responsibility for other things. In this particular case, the receiver is a delegate of a UITableView. So the first argument is telling you which table view is asking (which is the normal pattern for delegate protocols but not enforced and, anecdotally, something that beginners often omit when creating their own protocols) and the second argument is telling you what it is asking about.

A delegate may be responsible for multiple table views and at least one is all-but-certainly going to contain more than one index path, so both piece of information are needed.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • Well I guess I don't need to add another answer. However, here's the [link for said function and said protocol](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:cellForRowAtIndexPath:) – Arc676 Jan 08 '16 at 14:18
  • well, so this function/method has no name, only return type and arguments? – Martin Mlostek Jan 08 '16 at 14:21
  • @martynmlostekk Well, actually the name of the method is splitted and its parts are what you read before the colons (the params are what follows the colons). In your case, the name of the method is `tableView:cellForRowAtIndexPath:`. – Dree Jan 08 '16 at 14:25
  • okay, and how would i call this method in objective c? – Martin Mlostek Jan 08 '16 at 14:29
  • you just copy paste that method in your class. That;s it – Teja Nandamuri Jan 08 '16 at 14:38
  • You wouldn't call this particular method; a table view will call it when it needs a cell. But supposing that weren't the case: `[target tableView:p1 cellForRowAtIndexPath:p2]` where `p1` and `p2` are the variables you want to pass as parameters. Which, in Objective-C, are most likely also to be called `tableView` and `indexPath` because the convention is towards clear naming but I felt that would muddy the waters. – Tommy Jan 08 '16 at 14:41
0

The general form of a method definition in Objective-C programming language is as follows:

- (return_type) method_name:( argumentType1 )argumentName1 
joiningArgument2:( argumentType2 )argumentName2 ... 
joiningArgumentn:( argumentTypen )argumentNamen 
{
     body of the function
     }

A method definition in Objective-C programming language consists of a method header and a method body. Here are all the parts of a method:

Return Type: A method may return a value. The return_type is the data type of the value the function returns. Some methods perform the desired operations without returning a value. In this case, the return_type is the keyword void.

Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.

Arguments: A argument is like a placeholder. When a function is invoked, you pass a value to the argument. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method may contain no argument.

Joining Argument: A joining argument is to make it easier to read and to make it clear while calling it.

Method Body: The method body contains a collection of statements that define what the method does.

IN YOUR CASE

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

tableView is method Name

tableView cellForRowAtIndexPath is the name for second Argument (it is used to make a clear sense what our next argument will contain)

and at last this method will return a UITableViewCell.

Rahul
  • 5,594
  • 7
  • 38
  • 92
0

The UITableViewDataSource protocol is adopted by an object that mediates the application’s data model for a UITableView object. The data source provides the table-view object with the information it needs to construct and modify a table view.

Many methods take NSIndexPath objects as parameters. UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property), and to construct an index path from a given row index and section index (indexPathForRow:inSection: class method). (The first index in each index path identifies the section and the next identifies the row.)

Parameters

  • tableView:- A table-view object requesting the cell.
  • indexPath:- An index path locating a row in tableView.

Return Value

An object inheriting from UITableViewCell that the table view can use for the specified row. An assertion is raised if you return nil.

The returned UITableViewCell object is frequently one that the application reuses for performance reasons. You should fetch a previously created cell object that is marked for reuse by sending a dequeueReusableCellWithIdentifier: message to tableView. Various attributes of a table cell are set automatically based on whether the cell is a separator and on information the data source provides, such as for accessory views and editing controls.

Bhumesh Purohit
  • 491
  • 1
  • 8
  • 26