Ben Scheirman

Menu

Is UITableViewController Useless?

In my experience writing iPhone apps, I’ve come to the conclusion that `UITableViewController` is useless.

Since many of my readers aren’t iPhone developers, I’ll quickly describe what the UITableViewController is:

Almost all of the iPhone apps on your phone utilize some sort of table.  The table (which actually looks like a list) is implemented using a UITableView class.  This class has 2 properties that help define its behavior:

delegate datasource

The delegateis a reference to a class (any class) that implements methods that tell the table view how to react to user input, such as tapping or editing.  All of these methods are optional.

The datasourceis a reference to a class (any class) that implements methods that tell the table view how many rows it has, what the content for each row is, what the headers are (if any), sections, footers, etc.  2 of these methods are required, the rest are optional.

Most of the time, the delegateand datasourceproperties will be set to self in the controller which basically says: my view controller is providing all of this functionality.

So when you are wiring up your views in Interface Builder (or in code) you’ll frequently set it up so that it looks like this:

Screen shot 2010-03-31 at 6.09.43 PM 

This common scenario made a case for a reusable UITableViewController that set all of this up for you.  The UITableViewController does all this for you:

 Screen shot 2010-03-31 at 6.09.47 PM 

So that’s what UITableViewController is. now back to my post…

UITableViewController sounds great, right?  Less code is good… of course!  Well not if less code means you put the shackles on and can’t do something more advanced later on.

What if you want to add a toolbar to your view? 

What if you want to have a transparent background so that you can have a custom image show through, similar to the way I did with Pocket Tabs?

Well it turns out that you can’t.  Not easily anyway.  If you add a toolbar as a subview of a table view, then your toolbar will scroll with the content.  That’s not good.

Also if you try to get clever and re-parent the view controller class to point to a different UIView (one that has a toolbar & background image set properly) then the UITableViewController complains.

It breaks a pretty well-known software rule:  Liskov Substitution PrincipleUITableViewController inherits from UIViewController just like you’d expect.  The problem is, UITableViewController enforces that its view property be a table view instance.  It will crash at runtime if you set it to anything else.

 

Screen shot 2010-03-31 at 6.24.09 PM

Literally every time I figured I’d save some time by using UITableViewController it has come back to bite me.

Am I missing something?  Do you find UITableViewController useful at all?

Comments