简体   繁体   中英

UITableView delegate/datasource default value

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!

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM