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:

  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.

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.

UITableViewController

Works from the dawn of iPhone OS to today.

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