Ashish Agarwal
Ashish Agarwal

Reputation: 14925

iOS - What is the difference between Table View and Table View Controller

In the Object Library of Xcode, there are two options one can use to create table view - table view and table view controller. What is the difference between the two and when would they be used ?

Upvotes: 19

Views: 12890

Answers (2)

Rui Peres
Rui Peres

Reputation: 25917

The UITableViewController is a subclass of the UIViewController. It already assumes you will have UITableView as your rootView, so you already have access from the code to a tableView (self.tableView). It implements the UITableViewDataSource and the UITableViewDelegate protocol. It also gives you alot of methods for you to override. It allows you to not depend on XIB file, because you already know what you will have (UITableView as a rootView).

The UITableView is just UIView, normally you will have to comply to the protocols I have referenced above in your UIViewController in order to populate (data source) and work with it (delegate), and you probably have to create an IBOutlet for your UITableView.

On one hand you have speed but you are not as flexible as the other path. On the other you have the opposite.

Upvotes: 6

LJ Wilson
LJ Wilson

Reputation: 14427

A TableViewController is a ViewController with a TableView built in. This will have the delegate methods needed already declared and setup. This VC is already a TableView delegate and datasource. It cannot be resized. Upside is ease of use, downside is very limited flexibility.

A TableView is just that a TableView (subclass of UIView). It can be added to a ViewController and resized, used alongside another view based object, etc. The upside is the flexibility, the downside is that you have to setup the delegate and datasource methods yourself (in my opinion, well worth the time to get the flexibility).

One other note is that when using the new Static TableView cells (part of iOS5), you have to use a TableViewController.

Upvotes: 25

Related Questions