简体   繁体   中英

UITableView vs UITableViewController

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.

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.

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

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.

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" (eg 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM