13

Can someone explain to me the advantages of a UITableViewController over a UITableView? Generally, I use the controller but the basic table view seems a lot more flexible.

Tiago Mussi
  • 801
  • 3
  • 13
  • 20

6 Answers6

13

I always use UITableView. I never use UITableViewController.

Using UITableViewController is confusing. Each controller is supposed to do one screen except container controllers.

Well, UITableViewController is a controller and most of the time, a tableView is simply not the only thing there.

If you look at documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html

UITableViewController provide very little new interface compared to UITableView. None of which is essential.

– initWithStyle: 

(duh? I initiate my tableView in nib and I customized a lot anyway)

Getting the Table View

  tableView  property 

(the original tableView)

Configuring the Table Behavior

  clearsSelectionOnViewWillAppear  property 

(okay, just put some code on ViewWillAppear)

Refreshing the Table View

  refreshControl  property 

[tableView reload]?

So you see,

So unless you want your code to look neater (and not much more), there is no reason to use UITableViewController, that I know of.

Anonymous White
  • 2,149
  • 3
  • 20
  • 27
  • Only UITableViewController can have static content. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20 – DawnSong Jun 02 '15 at 08:01
  • 1
    Also, `UITableViewController` has some functionality (e.g., de-highlight on navigation pop) and setup already done for you, so you don't have to reinvent the wheel/tweak anything layout-related (especially post-iOS 7), and it works as expected from the start. I would only recommend your method if I need to share the screen with other subviews than the table. – Nicolas Miari Dec 21 '15 at 05:52
2

This is one thing I find very confusing about Cocoa: Apple's implementation of MVC (or at least the terminology they use) is questionable. In Cocoa, most views are themselves actually acting in some capacity as controllers insofar as the MVC pattern defines them (just look at UITableView--most of its methods are actually controller methods). Therefore you have Apple's additional concept of a view controller on top of what is already to some extent a controller. The fact that you don't even need to use UITableViewController at all (even with data binding) basically proves that the view is acting as its own controller. In MVC, you can't have a view bound to a model without a controller (that's the whole point of the controller in MVC).

It appears to Apple's developers, that a "view controller" (e.g. UITableViewController) is really more of a slightly higher-level helper/manager class that manages the more low-level UITableView class and enables a few key additional capabilities that pertain more to how the table view interfaces with the general UI of the app.

Additionally, it appears that Apple intends for a view controller to actually be more of a screen controller, recommending only one view controller per screen (presumably meaning you can't nest view controllers). I maintain that calling this concept a view controller is a misnomer.

Please don't hesitate to correct me on this anyone, I'm still very new to Obj-C/Cocoa myself.

devios1
  • 36,899
  • 45
  • 162
  • 260
2

UITableView is a view and UITableViewController is a controller that has UITableView as its root view.

So, by using UITableViewController:

  1. You have one root view that is UITableView itself. So it takes less memory than UITableView with a UIView as parent view in its controller.

  2. It provides you with a ready-to-use template, if you needed a controller and a UITableView as its main display view.

  3. Static table views are only valid when embedded in UITableViewController instances.

Jamie Birch
  • 5,839
  • 1
  • 46
  • 60
Sandeep Ahuja
  • 931
  • 6
  • 17
2

I don't think you understand your own question.

The UITableViewController is a controller.

The UITableView is a view.

One isn't better than the other, they're completely different objects, used for completely different things.

I'd spend some time learning the Model-View-Controller pattern so prevalent throuought Cocoa and the iPhone SDK.

The Model-View-Controller Design Pattern

August
  • 12,139
  • 3
  • 29
  • 30
  • 13
    I'm not sure if the OP meant this or not, but I have a similar question to what might have been meant: what are the benefits / drawbacks to using a UITableViewController vs. a UIViewController that contains a UITableView? – Bogatyr Oct 26 '10 at 11:33
  • 21
    I'm voting your answer down because THAT IS HIS question. Mine too. Who the heck voted this up to 14..? They are not COMPLETELY different - they are related and therefore the question is valid. When would you ever subclass a UITableView? What is a tableViewController used for compared to a tableView? I think I know the answer.. – badweasel Jun 21 '12 at 19:18
  • .. but if I knew that I knew I wouldn't have searched it. – badweasel Jun 21 '12 at 19:24
  • 1
    I think his question was more precisely asked here: http://stackoverflow.com/questions/9694185/uitableviewcontroller-vs-tableview – David Oct 06 '14 at 14:33
  • UITableViewController IS just a UIViewController + UITableView plus some minor code so Kieran question is absolutely valid and meaningful. This answer does not mach asked question at all. – vedrano Jun 02 '15 at 06:59
2

The idea is that ViewControllers hold a tree of subviews (and sub viewcontrollers) and manages dispatching events to/from its subviews. For example, you might have a view that holds an image, and that view lives in a view controller that handles orientation changes and memory warnings, etc.

Alex
  • 4,316
  • 2
  • 24
  • 28
0

The link above returns a "Page Not Found." For some reason, even the corrected link returns a "Page Not Found" when hyperlinked (perhaps Apple doesn't allow linking into their developer site).

To find the relevant information, go to Apple's Developer Connection (http://developer.apple.com/index.html) and search on "cocoa model-view-controller design pattern".

In the table of results, select the "Cocoa Fundamentals Guide: Cocoa Design Patterns." On that page scroll down the Table of Contents on the left side until you see the item "The Model-View-Controller Design Pattern." Select that item to read more details about that particular design pattern.

billmaya
  • 1,311
  • 3
  • 15
  • 34