Are there any advantages to using a UITableViewController over a UIViewController that implements tableview delegate and datasource methods? [closed]
Solution 1:
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:
- You need the table view to be smaller than the view controller's view.
- 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:
- Defines and setups up the
UITableView
. - Sets itself as the table view's dataSource and delegate.
- Overrides the
setEditing:animated:
method to also set theediting
property of the table view. - Deselects the last selected row in the
viewWillAppear:
method depending on theclearsSelectionOnViewWillAppear
property. - Flashes the table view's scrollbars in the
viewDidAppear:
method. - Hooks up the refresh control (as of iOS 6).
- Reloads the table view the first time it will appear.
- Adjusts the table view's
contentInset
(as of iOS 7). - Scrolls the table view as needed when the keyboard appears.
Solution 2:
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.
Works from the dawn of iPhone OS to today.
► Find this solution on GitHub and additional details on Swift Recipes.