简体   繁体   English

UITableView委托/数据源默认值

[英]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. 我注意到当我对UITableViewController进行子类化时,它的UITableView会自动将其委托/数据源设置为我的类。 Do I need to set the tableview delegate/datasource manually to my class just for code correctness or is that the expected behavior? 我是否仅出于代码正确性而需要手动将tableview委托/数据源设置为我的类,或者这是预期的行为?

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. UITableViewController类创建一个管理表视图的控制器对象。 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). 如果加载了包含表视图的nib文件,则数据源和委托将成为nib文件中定义的那些对象(如果有)。 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 . 如果未指定nib文件,或者如果nib文件未定义数据源或委托,则UITableViewController会将数据源和表视图的委托设置为self

in a UITableViewController, the delegate and datasource of the UITableView created by the ViewController is the ViewController by default. 在UITableViewController中,由ViewController创建的UITableView的委托和数据源默认为ViewController。

If you create your own UITableView then you have to assign the datasource and delegate yourself. 如果创建自己的UITableView,则必须分配数据源并委派自己。

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. 因此,它是在内部设置委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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