23

In Xcode when I create a new view controller to contain a tableview I have two options

  1. Create a new UITableViewController
  2. Create a new UIViewController that implements the UITableViewDelegate and UITableViewDataSource protocols

Assuming I properly implement all of the required and optional methods for the protocols, is there any advantage (besides not having to write the method stubs) to using the UITableViewController? means, is there anything (memory management, caching, etc.) implemented behind the scenes in the UITableViewController class that makes option 1 a better choice than option 2?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Wyatt McGuire
  • 477
  • 3
  • 9

5 Answers5

52

You have to write the delegate and protocol methods regardless of which of the two approaches you take.

There are only two possible reasons you should choose to use UIViewController over UITableViewController when you need a view controller with a table view:

  1. You need the table view to be smaller than the view controller's view.
  2. You need to add additional views to the view controller that don't scroll with the table view (though there are ways to solve this with UITableViewController).

Here are all of the things that UITableViewController does for you that you would need to replicate:

  1. Defines and setups up the UITableView.
  2. Sets itself as the table view's dataSource and delegate.
  3. Overrides the setEditing:animated: method to also set the editing property of the table view.
  4. Deselects the last selected row in the viewWillAppear: method depending on the clearsSelectionOnViewWillAppear property.
  5. Flashes the table view's scrollbars in the viewDidAppear: method.
  6. Hooks up the refresh control (as of iOS 6).
  7. Reloads the table view the first time it will appear.
  8. Adjusts the table view's contentInset (as of iOS 7).
  9. Scrolls the table view as needed when the keyboard appears.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 4
    Thanks, the list of "need to replicate" was above and beyond. – Dan Rosenstark Feb 03 '15 at 16:19
  • `UITableViewController` also handles adjusting the table view's `contentInset` and scrolling the first responder into view when the keyboard shows and hides. – rob mayoff May 14 '16 at 06:21
  • @robmayoff Good points. Though when this was originally answered, iOS 6 was the latest and nothing went behind the nav bars and tool bars back then so the `contentInset` didn't apply then. That changed in iOS 7. – rmaddy May 14 '16 at 06:45
  • The list still doesn't mention that the `contentInset` gets adjusted when the keyboard shows and hides. This allows you to scroll to and see the bottom of the table while the keyboard is up. This is different from #8 which refers to navbars and toolbars and #9 which refers to activating a textfield. – Janneman Jan 28 '17 at 11:25
  • I noticed that touching the status bar scrolls the table to top. However a plain 'UITableView' does this too so that doesn't count. Perhaps an idea to make a list of things that are the same? – Janneman Jan 28 '17 at 11:50
13

The Keyboard Advantage with 0 Lines of Code

UITableViewController provides automatic scrolling when an on-screen keyboard appears, while regular UIViewController doesn't.

A UITableViewController reliably moves the edited area within view, without the need to fiddle with keyboard notifications. It has done so since the dawn of iOS, while the keyboard notifications have changed, rarely providing backwards compatibility.

Whenever a view requires editing (like a login screen), consider using UITableViewController and capitalize on this unique feature with exactly 0 lines of code.

Unfortunately, a regular UIViewController adopting the UITableViewDelegate protocol does not offer that functionality.

UITableViewController

Works from the dawn of iPhone OS to today.

► Find this solution on GitHub and additional details on Swift Recipes.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Really?? I am using a UITableViewController and autoscrolling does not work. Is the toolbar in the bottom blocking this miraculous functionality? – Josh Mar 21 '16 at 09:45
  • The `UIToolBar` won't be in the way if it inside the `UITableView` hierarchy. If you managed to attach the bar to the bottom, you probably circumvented `UITableViewController`, hence the behavior you are reporting. Try the attached project and tinker with it! – SwiftArchitect Mar 22 '16 at 18:33
5

UITableViewController allows to have static table on iOS5

Amal T S
  • 3,327
  • 2
  • 24
  • 57
RolandasR
  • 3,030
  • 2
  • 25
  • 26
2

The only thing a UITableViewController has/does that a regular UIViewController doesn't, is a tableView property and it conforms to the UITableViewDelegate and UITableViewDataSource protocols. Ad RolandasR points out, setting a view controller to be a (descendant of) UITableViewController also lets you use static table cells.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
2

The main advantage of using the UITableViewDelegate is the simplicity. However, there are advantages to creating your own TableViewController that doesn't assume the view is a table:

1) You can add other elements and have the table take up a smaller part of the screen. 2) You can add a whole bunch of helper methods that are accessible to all VC that derive from your controller. I implement helper methods to automatically return row height when I have different UITableViewCell types and also auto-register the various cells, methods to get cell reference from a CGPoint in the table, pull-to-refresh callbacks, etc.

While you can always derive from UITableViewController and add helper methods, I think the main advantage is not having the table take up the whole view.

Καrτhικ
  • 3,833
  • 2
  • 29
  • 42