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
UITableViewController is a UIViewController. Its view is a UITableView, can’t change constraints. You can’t add view into
UITableView, without a cell.
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
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
Free TableView is a
UIViewController and add a
UITableView into the view. You can easily set constraints for
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.
Not accept static table view. I have a trick for this: use Dynamic table view, but datasource is a collection of
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.
I have a demo for this. Download here. Some code in demo I need to tell you.
knControlleris UIViewController. I made my own controller to manage building UI by code.
knCustomTableControlleris Free TableView
fill:toView:spaceare my Auto Layout Libs, called knContrainsts.
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