yonzhang
yonzhang

Reputation: 65

Does UITableViewController equal to UITableViewDataSource, UITableViewDelegate

Could we use UITableViewDataSource, UITableViewDelegate totally replace UITableViewController, for example, is there difference between these two code lines?

@interface ShareViewController : UITableViewController
@interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

Upvotes: 3

Views: 1420

Answers (5)

rptwsthi
rptwsthi

Reputation: 10172

UITableViewController is a UIViewController already implemente UITableViewDelegate, UITableViewDataSource protocols, thus when you inherit your class with it you don't need to implement them in your ViewController again. It's like:

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

To make it more clear, UITableViewDelegate, UITableViewDataSource are protocols, whereas UITableViewController is ViewController.

Upvotes: 0

blub
blub

Reputation: 1014

UITableViewController most importantly is a UIView Controller.

It implements UITableViewDataSource and UITableViewDelegate protocols and has a tableView property/outlet (which is his only .view).

A UITableView is a UIView.


To get a UIViewController to do nearly everything a UITableViewController does for you, you have to implement the two protocols, drag a UITableView inside your UIViewController, create an outlet for it and set your UIViewController as the delegate and dataSource for your tableView outlet

You can add a UIRefreshControl to a UITableViewControllers tableView by just clicking a checkmark in StoryBoard for iOS6+, you have to write some more lines to do this on a UITableView inside a UITableViewController

Upvotes: 1

Peter Warbo
Peter Warbo

Reputation: 11718

If you check the reference for UITableViewController you will see that it conforms to UITableViewDataSource and UITableViewDelegate but merely conforming to the same protocols does not make the classes equal. UITableViewController is a class suited for using with UITableViews where UIViewController can be used with any UIView.

Upvotes: 0

Andrea Mario Lufino
Andrea Mario Lufino

Reputation: 7921

There is a BIG difference between these two lines of code. This

@interface ShareViewController : UITableViewController

means that your ShareViewController inherits from UITableViewController, so it provides, for example, an UITableView as main view. This

@interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

means that your ShareViewController is a classic UIViewController (inherits from UIViewController) and simply implements protocols to manage table views.

Upvotes: 3

Girish
Girish

Reputation: 4712

@interface ShareViewController : UITableViewController

Above line means ShareViewController is inherits from UITableViewController

@interface ShareViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

Above line means ShareViewController inherits from UIViewController & implements the UITableViewDataSource, UITableViewDelegate delegates

Upvotes: 0

Related Questions