UITableViewController vs UIViewController + UITableView
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 special UIViewController
. Its view is a UITableView, can’t change constraints. You can’t add view into UITableView
, without a cell.
Advantages
- 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
-
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.
-
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
. -
Can't add sticked top or bottom view like below
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.
- Not automatically move
contentOffset
to the current active text field.
Fix disadvantages
This is a trick. I write in detail later:
- Add
UITableView
, withdatasource
is a array ofUITableCell
. - Add a library to handle textfield activated. I suggest Håkon Bogen's lib (download at Github).
Explain for demo
I have a demo for this. Download here.
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, nameknContrainsts
. - 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.UITableViewController
still has good practice which can’t replace byFree TableView
- Understand the differences can help us choose the right one for our situation.
Enjoy coding.