5

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 Syntax in Objective C

Cœur
  • 37,241
  • 25
  • 195
  • 267
Doug R
  • 5,749
  • 2
  • 28
  • 32
  • You should learn Objective-C first, you won't regret it. Read this book and do all the exercises: http://www.amazon.com/dp/0321566157 – Rob Keniger May 26 '10 at 07:12

3 Answers3

4

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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • And an implicit `_cmd` parameter that rarely gets used. – dreamlax May 26 '10 at 02:24
  • What's the _cmd parameter for? – Shiki May 26 '10 at 02:25
  • 1
    @Shiki: I think, the `_cmd` argument indicates the selector that was used to resolve the method implementation. In rare situations where the same method implementation is shared among multiple selectors, the `_cmd` argument can be used to distinguish how the method implementation was invoked. Aside from runtime mangling, I've never actually used `_cmd`. – dreamlax May 26 '10 at 03:08
2

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

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.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398