PDA

View Full Version : Which is method name?




mikezang
Jul 7, 2010, 03:48 AM
I know method in Objective-C as defined as below:
- (void) classMethod: (int) first secondArgument:(int) second;
-= Method type
(void)= return type
classMethod= method name
(int)= first argument type
first= first argument name
secondArgument:= second argument label
(int)= second argument type
second= second argument

But I am not sure what is the method name as below code, I thought method name should be numberOfRowsInSection and return type is (NSInteger), but I am in confuse about tableView:(UITableView *)tableView, what are they?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of time zone names.
return [timeZoneNames count];
}



robbieduncan
Jul 7, 2010, 03:59 AM
The method name (as you would use in @selector for example) is tableView:numberOfRowsInSection:

mikezang
Jul 7, 2010, 04:13 AM
so how this method be called?

robbieduncan
Jul 7, 2010, 04:18 AM
Normally you wouldn't call it: it's a delegate method that will automatically be called by the UITableView. If you are struggling to understand this then I'm sorry but I don't have the time or energy to teach Objective-C basics. Apple have a very good document (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf) that covers the language.

To directly answer the question if we assume <i>instance</i> is an instance of the class implementing the method, <i>tableInstance</i> is the UITableView instance we are dealing with and <i>section</i> is the section we are interested in then the message syntax is:

NSInteger rowsInSection = [instance tableView:tableInstance numberOfRowsInSection:section];

mikezang
Jul 7, 2010, 04:29 AM
Thanks for your answer.
I am reading that document what you suggested, but I couldn't find what I need so that I asked such simple question:(

This way can only be used for message syntax, is it right?

If use dot syntax, I think it should look like as below, do you think so?
- (NSInteger)numberOfRowsInSection:(UITableView *)tableView sectionInTable:(NSInteger)section {
// Return the number of time zone names.
return [timeZoneNames count];
}

robbieduncan
Jul 7, 2010, 04:31 AM
If use dot syntax, I think it should look like as below, do you think so?
- (NSInteger)numberOfRowsInSection:(UITableView *)tableView sectionInTable:(NSInteger)section {
// Return the number of time zone names.
return [timeZoneNames count];
}

I don't see what this code has to do with dot syntax. Dot syntax is only used for properties.

mikezang
Jul 7, 2010, 04:38 AM
Yeah, your are right, dot syntax is only for properties.

But I am still not sure about label of second argument, that doesn't explain the meaning of that argument...
numberOfRowsInSection:section

robbieduncan
Jul 7, 2010, 04:43 AM
Yeah, your are right, dot syntax is only for properties.

But I am still not sure about label of second argument, that doesn't explain the meaning of that argument...
numberOfRowsInSection:section

The meaning of any argument is 100% down to the exact method being called. In this case it is the section that the table view wants to the number of rows in. The argument is simply section (a NSInteger). The numberOfRowsInSection is simply a name that is human readable to indicate to the caller what this argument is for (to prevent questions like this).

Luke Redpath
Jul 7, 2010, 06:24 AM
I don't see what this code has to do with dot syntax. Dot syntax is only used for properties.

You can use dot syntax for any getter method, it doesn't have to be a declared property. Whether you do or not is a matter of style but I tend to follow the rule of using dot syntax for queries but not commands. So I would use someArray.count but [someMutableArray removeAllObjects].

robbieduncan
Jul 7, 2010, 06:28 AM
You can use dot syntax for any getter method, it doesn't have to be a declared property. Whether you do or not is a matter of style but I tend to follow the rule of using dot syntax for queries but not commands. So I would use someArray.count but [someMutableArray removeAllObjects].

Sure but I don't think you can use if for methods that take multiple arguments?

Luke Redpath
Jul 7, 2010, 06:29 AM
Sure but I don't think you can use if for methods that take multiple arguments?

Nope, you can't. Perhaps its clearer to say I use dot-syntax for property-ish queries (which may or may not declared properties).