-2

I need some help with understanding the class declaration syntax in Objective C, or to be more specific

@interface SomeViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

what does UITableViewDataSource,UITableViewDelegate mean

My understanding is that it receives these two objects when the class is instantiated.Correct me if I am wrong..

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
Anoop
  • 5,540
  • 7
  • 35
  • 52
  • actually `UITableViewController` already conforms to the protocols `UITableViewDataSource` and `UITableViewDelegate`. So specifying them here is redundant – user102008 Sep 28 '12 at 01:34

3 Answers3

2

UITableViewDataSource and UITableViewDelegate are two protocols, not classes (or objects).

When you declare a class, you can specify any number of protocols that your class implements using the < > bracket syntax.

A protocol is a list of required or optional methods. Adding the protocol to the class declaration doesn't actually implement or declare any of the methods in that protocol. You have to do that yourself. However if you don't implement a required protocol method, you will get a compiler warning.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

When you declare a class, the <> syntax allows you to specify a list of protocols the class must comply with.

A protocol is a "set" of methods that your class must implement (You can specify optional methods too). They only have method declarations, but the programmer must implement them in his classes. Protocols are really important in Objective-C since they are the heart of the delegation pattern.

In this specific case, UITableViewDelegate is a protocol that an object that deals with UITableViews must comply with. Table View delegates are responsible for controlling the table and it's cells, such as setting their heigh, accesories, etc.

UItableViewDataSource is a protocol an object that gives data to a table view must comply with. An object complying with this protocol is responsible for returning the data that will be displayed in a table view.

Not using the protocols when needed can create warnings that will sooner or later crash your app.

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
0

UITableViewDataSource and UITableViewDelegate are protocols. To understand what protocol is see this. Protocols are similar to interface in Java.

@interface SomeViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

This line just means that you declare a class "SomeViewController" which inherits from "UITableViewController" and adopts two protocols: UITableViewDataSource and UITableViewDelegate

nagan
  • 119
  • 3