Oliver
Oliver

Reputation: 11607

Syntax for implementing a protocol in Objective C, as compared to C#?

Unexpected events at my company have resulted in my being drafted in to write and release an iphone app in a short amount of time, though I have no experience in Objective C. I'm having trouble following some of the syntax:

I have the following method definition:

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

}

It lives in a class that is defined with this interface:

@interface TableExampleViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *lightbulbs;
}

What does the method declaration mean? I understand that UITableViewCell *) is the return type, cellForRowAtIndexPath is the method name, and (NSIndexPath *) indedPath is the parameter, but what is the significance of tableView:(UITableView *)tableView? I assume it has something to do with the protocols that the class implements. What would be the equivalent syntax in c#?

Upvotes: 1

Views: 264

Answers (3)

fengd
fengd

Reputation: 7569

tableView: cellForRowAtIndexPath:

is the method name, it different from c#'s naming. in c#, the method definition might be

UITableViewCell GetCellFromTableViewAtIndexPaht(UITableView tableView, NSIndexPath indexPath)

Upvotes: 2

DrummerB
DrummerB

Reputation: 40211

This is the Delegate Design Pattern. Basically the UITableView instance in your UIViewController class asks your view controller to provide a UITableViewCell for the specified index. So let's say you have an array of songs in your view controller and you want them to appear in the table view. Then you would create a new UITableViewCell, set it's textLabel.text to the title of the song at the specified index (indexPath.row) and return the cell. Because you are supposed to reuse UITableViewCells for performance reasons, I recommend reading the Table View Programming Guide in the documentation, especially Populating a Table View

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

In Objective-C method name includes all argument names, so the name of the method is

tableView:cellForRowAtIndexPath:

which includes both colons :.

It has nothing to do with protocol implementations - it is simply the way the methods are named in Objective-C. So an imaginary C# equivalent would be simply

UITableViewCell tableViewcellForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath)

Upvotes: 4

Related Questions