Are There Any Advantages to Using a Uitableviewcontroller Over a Uiviewcontroller That Implements Tableview Delegate and Datasource Methods

What are the benefits of using tableView within a UIViewController?

1- Ability to customize the tableView height and width as you want

2- Ability to have more than 1 tableView instead of only one with UITableViewController

3- When adding a subview it's added to self.view which won't make that subview scroll with tableView like in UITableViewController

UITableViewController vs TableView

Populating a UITableView inside of a UIViewController is no different than populating a UITableView inside of a UITableViewController. You just have to make sure you implement the required datasource and delegate methods. You also need to be sure to assign the UIViewController as the delegate and the datasource for that UITableView.

If you want objects other than the table, then you should us a UIViewController. In fact, I rarely use the UITableViewController any more just in case I need to add other objects.

What is the difference between UIViewController and UITableViewController

I believe all of the behavior UITableViewController adds is well defined in the class documentation: https://developer.apple.com/documentation/uikit/uitableviewcontroller?language=objc

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

• If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.

• If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

• When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

• When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

• It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.

All of these are behaviors which should be easy to re-implement if they apply to your specific controller and table view.

In general I have found it preferable to implement these behaviors myself in order to allow for alternate inheritance hierarchies as you noted and because I usually consider setting both the delagate and datasource of a table view to be a view controller a design smell. Those are independent concerns which usually can and should be handled by some other class (e.g. a dedicated data source for a particular model class) rather than bloating a view controller.

Have set datasource and delegate of TableView inside ViewController but nothing happening

The reason is because you are not implementing your – tableView:numberOfRowsInSection: method. Implement this method and return the amount you need.

Data Source and Delegate Options

You don't need to conform to both protocols and you don't need to set the delegate and dataSource in Storyboard, it can be done programatically as well.

You only need to do these if you are working with a UITableView in a ViewController that is not subclassing UITableViewController. UITableViewController automatically takes care of conforming to both protocols for you.

You need to set up the delegate/dataSource to a certain ViewController, because the system needs to know, which class' delegate/dataSource methods it needs to call when setting up the table view. If you have several view controllers with table views inside them in the same Storyboard, without setting up the delegate/dataSource to the current view controller, the system wouldn't know which class it needs to call for which tableview.

Conformance to UITableViewDelegate protocol is optional, if you only need to display data on a table view, but don't need user interaction with it, you only need to conform to the UITableViewDataSource protocol. This is needed in order to set up the number of sections/cells in the table view and to set up the content of these as well.

How to use UIScrollViewDelegate methods on a tableView added to a UIViewController?

UITableViewDelegate inherits from UIScrollViewDelegate.
If you determine that object is delegate of table view, it would be also a delegate of scrollView.



Related Topics



Leave a reply



Submit