首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我必须将dataSource和委托设置为self?

为什么我必须将dataSource和委托设置为self?
EN

Stack Overflow用户
提问于 2016-08-28 15:13:05
回答 4查看 10K关注 0票数 14

我正在学习swift,我正在学习的课程是教tableViews的。我必须将TableViewController设置为包括UITableViewDataSourceUITableViewDelegate。然后,在viewDidLoad中,我必须设置

代码语言:javascript
运行
复制
tableView.dataSource = self
tableView.delegate = self

为了让tableView出现并加载数据。

为什么我必须这样做?

EN

Stack Overflow用户

发布于 2018-01-03 06:29:24

我将对此提供详细的解释。在这里,UITableViewDataSourceUITableViewDelegate实际上是协议。不幸的是,UIKit Framework不是开源的。但我可以向你保证,在参考了许多文章之后,这是内部发生的事情。

协议就像篮球教练,里面有一些要求。他/她通过使用这些需求来告诉玩家类、结构、枚举what to do?。但是他/她自己doesn't knows how to do?。因此,符合该协议的类或结构应该在实现扣篮的同时实现这些要求。

代码语言:javascript
运行
复制
protocol UITableViewDelegate {
 func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
}

一个协议被称为DataSource协议,那么它总是包含带有“返回类型”的必需函数,如下所示。

代码语言:javascript
运行
复制
protocol UITableViewDataSource {
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
}

在自定义viewController中实现UITableView

代码语言:javascript
运行
复制
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()    

    override func viewDidLoad {
      tableView.delegate = self
      tableView.dataSource = self
    }

在这里,tableView充当委托者(发送者),viewController object i.e (self)充当委托者(接收者)。

现在称为tableViewUITableView()对象在UITableView()类中有两个可选的存储属性,分别是协议类型&现在它被tableView称为

代码语言:javascript
运行
复制
tableView.delegate: UITableViewDelegate?
tableView.dataSource: UITableViewDataSource?

为了在viewController中获取UITableView,.It应该同时符合这两个协议。因此,viewController类对象已经实现了这两个协议所需的所有功能。现在self既可以用作UITableViewDelegate类型,也可以用作UITableViewDataSource类型,因为协议可以用作符合它的类的对象的类型。现在,tableView的两个属性,即delegatedataSource都被分配给self,因为它具有相同的各自协议类型。

两个协议的非可选功能都在viewController类对象中实现,如下所示

协议UITableViewDelegate函数

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
// Do further processes like pushing or poping another viewController
}

协议UITableViewDataSource函数

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
 }

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
 }

1)当用户选择某一节中的一行时,tableview(发送方)即UItableView()调用如下所示的UITableViewDelegate函数,方法是将数据传递给参数tableViewindexPath,该参数通过delegate属性驻留在viewController对象(接收方)中。现在,viewController使用这些传递的数据进行进一步的处理,比如推送或弹出到新的自定义viewController。

代码语言:javascript
运行
复制
tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

2) UITableViewDatasource协议内部的函数向tableview(发送方)提供自定义数据。tableview通过调用数据源函数来请求viewController对象,并通过datasource属性将数据传递给参数tableViewindexPath,该参数驻留在viewController对象(接收器)中。现在,viewController使用这些传递的数据并将自定义数据返回给tableview。现在tableview使用这些数据在一个部分中创建"10“个单元格&类似于indexpath中的”单元格“

代码语言:javascript
运行
复制
tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"

tableView.dataSource?.tableView(UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> returns "cell"

因此,我们将tableView.delegatetableView.Datasource都赋值为self。它不符合协议。而是用self初始化属性tableView.delegatetableView.Datasource。因此,tableView可以通过它们调用驻留在self中那些DelegateDatasource方法。

一个类只要在其中实现它的所有需求,就可以说是符合协议的。这就是它,但不是通过将self赋值给其他属性。

最后,整个UIKit Framework在其所有类(如UIApplicationUITableViewUICollectionViewUITextField等)中使用委托和数据源设计模式进行数据通信。不幸的是,UIKit Framework不是开源的。

票数 4
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39188847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档