Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Glama

macrumors member
Original poster
Feb 5, 2009
31
0
Hi,
I came across the code below in a book on iphone development:
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

[some code here]

return rows;
}
I dont understand the first line. I understand from the accompanying text in the book that numberOfRowsInSection is the name of the method, so I guess the rest of the line means that it accepts a NSInteger which it refers to as "section". But the first half of the line baffles me. Since the method returns a variable called "rows", I assume that is an integer so Im thinking the "NSInteger" at the beginning of the line specifies the return type. So what is the "tableview:(UITableView *)tableView" part for? What does it specify?

Phil
 
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
The method returns an NSInteger and takes two parameters - one is a pointer to a UITableView object, the other is an NSInteger indicating the section in that UITableView that you are to return the number of rows of.
 
Thanks Troglodyte.
So one parameter comes before the method name, and one comes after. As a newbie to objective-c, that seems so odd. Any way to make sense of that? What if it took 3 input parameters? Where would the next one go?
 
- (NSInteger) = Return Type
tableView: = Method Name
(UITableView *) = Data Type it accepts (In this case it accepts a pointer to a UITableView)
tableView = Parameter Name
numberOfRowsInSection: = Method Name with Second Parameter
(NSInteger) = Data Type it accepts (In this case it accets NSInteger which is a wrapper class for int)
section = Parameter Name

*if there was a third parameter, it would look like this...
{

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section someOtherParameter: (NSInteger) someOtherInteger {
 
Glama, you need to get a book on learning Objective-C and another one on learning iPhone development. You will learn more quickly that way.
 
Thanks Troglodyte.
So one parameter comes before the method name, and one comes after. As a newbie to objective-c, that seems so odd. Any way to make sense of that? What if it took 3 input parameters? Where would the next one go?

Actually the name of the function is 'tableView:numberOfRowsInSection:' and takes 2 parameters.
 
Glama, you need to get a book on learning Objective-C and another one on learning iPhone development. You will learn more quickly that way.

I have one, but it focuses on bigger concepts like inheritance, etc. I havent found a good source of info on the actually syntax and "grammar" of objective-C.
 
Thanks FarSeide and lloyddean. Obj-c is tough to get used after years of

integer mymethod(param1, param2)
 
What books do you have?

Have you read the sticky: iPhone Developer FAQ?

This is almost the same as Objective-C:

Code:
integer mymethod(type param1, type param2)

and 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

integer == NSInteger

mymethod == tableView:numberOfRowsInSection:

type param1 = UITableView*

type param2 = NSInteger

param1 = tableView

param2 = section
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.