UITableViewController vs UIViewController + UITableView

A small compare advantages and disadvantages in TableViewController

Posted by Ky Nguyen on October 12, 2017

Have you ever used UITableViewController? Have you ever used UIViewController and UITableView? What are the differences between these two solutions? What are good practice for them? This note is a quick compare about UITableViewController and UIViewController + UITableView. I will call the UIViewController + UITableView is Free TableView

UITableViewController

UITableViewController is a UIViewController. Its view is a UITableView, can’t change constraints. You can’t add view into UITableView, without a cell.

Advantages

It’s good at handling text field become first responder. UITableViewController will automatically move contentOffset to the current active text field. Good practice for chat log screen, form screen. Accept static table view: very easy and fast to design form with Storyboard

Disadvantages

It’s bad for state view (empty state, loading state, error state). Can’t display state view by adding to view. You can do that by adding to tableHeaderView or tableFooterView, but you have to handle much things. It’s bad for sticked top or bottom view.

Can’t inherit from BaseController. You have a BaseController with many setting inside, such as, stateView, loadData, setupUI, etc… and you can’t inherit that with UITableViewController.

Free TableView

Free TableView is a UIViewController and add a UITableView into the view. You can easily set constraints for UITableView.

Advantages

Very flexible. You can show/hide the tableView, set padding for tableView. Good for state display. In the demo, I show an empty state is a green UIView, at the center of the screen.

Disadvantages

Not accept static table view. I have a trick for this: use Dynamic table view, but datasource is a collection of UITableCell. But it will not automatically adjust when the textfield activate. We need lib to help us take care of it. I suggest this lib from Håkon Bogen (download at Github). Really good lib.

Explain for demo

I have a demo for this. Download here. Some code in demo I need to tell you.

  • knController is UIViewController. I made my own controller to manage building UI by code.
  • knCustomTableController is Free TableView
  • Some functions like horizontal:toView:space, fill:toView:space are my Auto Layout Libs, called knContrainsts.
  • Empty state in demo is just a green UIView. I don’t want to add more code to make it more complicated.

Conclusion

Free TableView has disadvantages but can be solved easily. This solution is much more flexible and easier to manage. But UITableViewController still has good practice which can’t replace by Free TableView