11

Sometimes I want to subclass UIViewController for some app wide customizations. Eg. something that all view controllers should perform during viewDidLoad or viewWillAppear or so.

Naturally I subclass UIViewController and go from there and all view controllers inherit from that. But some of the controllers run tables. And there is UITableViewController designed for that purpose.

So I subclass UITableViewController too and just do the same things there. Which does not seem to be the smartest thing in OOP terms. And there is no multiple inheritance etc.

And as UITableViewController inherits from UIViewController ...

Now I am asking myself why I don't just create my own table view controller that inherits from my very own view controller subclass and adds all the table stuff. But what is "all the table stuff"?

  • There is the skeleton code that xcode adds to every new table view controller. Quite handy but that can be easily moved to code snippets.
  • There is the declaration of the protocols UITableViewDelegate and UITableViewDataSource. Easy going. The implementation of those methods has to follow in each subclass of UITableViewController anyway.
  • There are probably reasonable default implementations of all those mandatory methods in the protocol. Such as returning 0 for numberOfSectionsInTableView: or nil for titleForHeaderInSection or 44.0f for heightForRowAtIndexPath: (Bad example though. Could be smarter not implementing that at all)

So despite the obvious stuff, are there any miracles that UITableViewController takes care of?

1
  • Thanks to everybody who answered. This helps a lot on this certain architecture decision. Jun 26, 2013 at 7:29

3 Answers 3

11

I believe all of the behavior UITableViewController adds is well defined in the class documentation: https://developer.apple.com/documentation/uikit/uitableviewcontroller?language=objc

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

• If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.

• If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

• When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

• When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

• It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.

All of these are behaviors which should be easy to re-implement if they apply to your specific controller and table view.

In general I have found it preferable to implement these behaviors myself in order to allow for alternate inheritance hierarchies as you noted and because I usually consider setting both the delagate and datasource of a table view to be a view controller a design smell. Those are independent concerns which usually can and should be handled by some other class (e.g. a dedicated data source for a particular model class) rather than bloating a view controller.

4
  • Thank you. Sounds doable and like a good invest for futuere projects too. Are you aware of any good code exaple that could subtitute the UITableView? Jun 26, 2013 at 7:31
  • @Jonah How do you manually implement the last three bullets in UIViewController (i.e. clear selection every time table view is displayed, flash the table view's scroll indicators when the table view has appeared, and implement the superclass method setEditing:animated: o toggle the edit mode of the table)? Currently my cells stay highlighted when I return to the tableview, the scroll bars don't flash, and pressing the Edit button does absolutely nothing (it does not even change the text of the button to Done). Could you please edit to your answer with links or explanations of how to do these?
    – golddove
    Jun 25, 2014 at 7:12
  • @golddove I'm reluctant to offer solutions which skip over understanding the problem at hand so I'm not sure that code examples would be useful here. If I were unsure how to implement flashing scroll indicators I might start by looking at the class documentation for UITableView, that doesn't offer any immediate solution but I see it is a subclass of the more general UIScrollView. UIScrollView has a flashScrollIndicators method. Calling that method inside a controller's viewDidAppear reproduces the behavior seen in a UITableViewController.
    – Jonah
    Jun 25, 2014 at 14:35
  • yes but what about interface builder / NIB / Storyboard ? I've tried what they've mentioned and it crashes.
    – horseshoe7
    Sep 22, 2014 at 11:26
3

So despite the obvious stuff, are there any miracles that UITableViewController takes care of?

Not that I am aware of. As far as I know UITableViewController is mostly a convenience class that can be replaced with your own subclass that adds a few lines of code.

Apple's class documentation pretty much says all that UITableViewController does (and I'm not repeating that here because it may well change in the future). Sometimes, additional information can be gleaned from a class' header file, but in the case of UITableViewController.h the source code comments just repeat what is already in the class docs.

In the end you must decide yourself what you want to replicate in your own subclass. Maybe your project does not need .nib handling? Or none of your table views is editable? So just don't code that...

0

It seems that the UITableViewController takes care of a lot of management issues that really you could just do for yourself, if you wanted to. Check out the documentation -- it will automatically create a UITableView for you, reload it, etc.

1
  • "UITableView takes care of a lot of management issues" - I suppose you've meant UITableViewController? Besides, the author pointed out, that he means 'beside obvious stuff' you're writing about.
    – Nat
    Jan 29, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.