2
votes

Simple question. I noticed that when I subclass UITableViewController, it's UITableView automatically sets it's delegate/datasource to my class. Do I need to set the tableview delegate/datasource manually to my class just for code correctness or is that the expected behavior?

Thanks!

3

3 Answers

5
votes

Yes it is the official expected behavior.

This is detailed here in the documentation, especially in the paragraph "Overview":

Overview

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

  • [...]

  • 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.

2
votes

in a UITableViewController, the delegate and datasource of the UITableView created by the ViewController is the ViewController by default.

If you create your own UITableView then you have to assign the datasource and delegate yourself.

0
votes

Yes..it will automatically set the delegates methods. as it is doing that internally. It does in case of coding implenmentation

As the documentation says :

 @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

So, it is internally setting the delegate methods.