开发者

Can somebody explain this Objective-C method declaration syntax

I'm working through an iPhone development book* without really knowing Objective C. For the most part I'm able to follow what's going on, but there are a few method declarations like the one below that I'm having a bit of trouble parsing. For example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
  return [self.controllers count]; //controllers is an instance variable of type NSArray in this class
}

It looks this is a method called numberOfRowsInSection, and it returns an NSInteger, and takes an NSInteger as a parameter which is locally called 'section'. But I don't understand all the references to tableView, or why this takes a parameter when it is not used within the method. Can somebody clarify this? Thanks.

*p. 258, Beginning iPhone 3 Development, by Mark and LaMarche, published by Apress

Update: I was able to find another SO thread that goes into a bit more detail: Method 开发者_如何学PythonSyntax in Objective C


This is a method called:

tableView:numberOfRowsInSection:

It takes two parameters:

  • a UITableView*
  • a NSInteger

The method also takes an implicit self parameter , which is the instance it is called with. As dreamlax notes, it also takes an implicit _cmd, which is the method that currently gets invoked.

As Mark says, it is completely common to not use certain parameters if you are conforming to a certain interface.


This is a method called tableView:rowsInSection: that UITableView specifies for its delegates. The tableView argument is there in case you have one controller in charge of several UITableViews, so that it can tell which one is talking to it. It's also useful if you need to query the UITableView for information in order to decide what you want to do. This is very common in delegate methods.


This method is conforming to the UITableViewDataSource protocol. If you're familiar with C# or Java, a protocol is like an interface.

It's completely legal and not too abnormal for a method conforming to an interface or protocol to ignore some of the arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜