Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBenny Chew
Courses Plus Student 2,717 PointsQuestion about code generating the table cells
Hi, I don’t really understand the method for generating the table cells. The instructor didn’t really explain this. Can someone help me?
```- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}```
What is the asterisk for right after - (UITableViewCell *)tableView
and why is it not place before the method name like this: - (UITableViewCell)*tableView
?
I understand this code: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
makes sure the cells get generated and the other 2 lines defines the data in the cells, right?
2 Answers
Patrick Serrano
13,834 PointsThe - (UITableViewCell *)
is saying that the method returns a pointer to a UITableViewCell.
If we break down the parts of the method I think it will make more sense.
This is the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)
says what the method returns. In this case it is a pointer to a UITableViewCell, but it could be anything including void. It just depends of the method and what it does.
tableView:(UITableView *)tableView
the tableview: is part of the method name. The (UITableView *)tableview
is the parameter. The part in the parentheses again is specifying the type that the parameter is, and in this case that is a UITableview. The tableview
following the parentheses is the parameter name. This is what you use to access the UITableview that was passed into the method.
cellForRowAtIndexPath:(NSIndexPath *)indexPath
the cellForRowAtIndexPath:
is part of the method name. The part of (NSIndexPath *)indexPath
in parentheses again is specifying the type of the parameter being passed in and the word indexPath after is the parameter.
I hope that helps.
Benny Chew
Courses Plus Student 2,717 PointsThanks for the explanation Patrick Serrano. I will study it in more depth, especially how you write methods in Objective C (I have an ActionScript 3 background). Perhaps it looks more complicated than it is. :)
Patrick Serrano
13,834 PointsBe sure to do the Objective-C Basics course if you haven't. They start with the basics of C, and move onto Objective-C and give you a pretty broad foundation from which to build on. The videos in the last section where Amit explains pointers is really helpful.