在Swift中符合UITableViewDelegate和UITableViewDatasource? [英] Conforming to UITableViewDelegate and UITableViewDatasource in Swift?

查看:567
本文介绍了在Swift中符合UITableViewDelegate和UITableViewDatasource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习swift 2.0,我想知道你是否还需要添加代码 tableView.datasource = self tableView.delegate =自我像在Obj-C中一样符合协议?

I am learning swift 2.0, and I was wondering if you still need to add the code tableView.datasource = self and tableView.delegate = self like in Obj-C to conform to the protocols?

示例代码:

class AboutViewController: UIViewController, UITableViewDataSource,   UITableViewDelegate
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // conform to protocols
        aboutTableView.dataSource = self
        aboutTableView.delegate = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 2
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return 50
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        // Code here
    }
}

现在表视图加载每个单元格中的正确数据。

Now the table view loads with the correct data in each cell.

但是,如果我删除 aboutTableView.datasource = self aboutTableView.delegate = self viewDidLoad ,我的表格视图为空白。这是为什么?

However, if I remove aboutTableView.datasource = self and aboutTableView.delegate = self from viewDidLoad, my table view is blank. Why is this?

这段代码是否仍然需要,因为我看到许多youtube教程不再包含在swift中了,我很困惑为什么我的没有没有它工作?

Is this code still required because I see many youtube tutorials that does not include this anymore in swift, and I'm confused as to why mine doesn't work without it?

推荐答案

首先,这完全独立于你使用哪种语言
,Swift或Objective-C。

First of all, that is completely independent of which language you use, Swift or Objective-C.

但是有两种不同的情况可能导致混淆:

But there are two different cases which may cause the confusion:

A UITableViewController 子类:

A UITableViewController subclass:

UITableViewController 已经符合 UITableViewDataSource UITableViewDelegate 。它有一个 tableView 属性,
dataSource 委托属性已设置为 self

UITableViewController already conforms to UITableViewDataSource and UITableViewDelegate. It has a tableView property, whose dataSource and delegate property are already set to self.

在您的子类中,通常覆盖
数据源和委托方法。

In your subclass, you typically override the data source and delegate methods.

一个 UIViewController 带有<的子类code> UITableView property:

A UIViewController subclass with a UITableView property:

这里你定义了一个 UITableView 中的$ c>属性并在代码中初始化它,或者
将它连接到界面构建器中的表视图。

Here you have defined a UITableView property in your subclass and initialize it in your code, or connect it to a table view in the interface builder.

In在这种情况下,你必须设置tableview的 dataSource 委托
属性,无论是在代码中还是在接口
构建器,如 luk2302的答案中所述。

In this case you have to set the dataSource and delegate property of the tableview, either in code or in the interface builder, as explained in luk2302's answer.

如果数据源和委托是视图控制器本身,
则必须明确声明协议一致性,
an d 实现
数据源和委托方法(但没有覆盖
a超类方法)。

If data source and delegate are the view controller itself, then you have to declare the protocol conformance explicitly, and implement the data source and delegate methods (but without overriding a superclass method).

当然,在这两种情况下,表视图数据源和委托
都可以设置为不同的对象,它确实不必是视图控制器本身的

Of course, in both cases, the table view data source and the delegate can be set to a different object, it does not have to be the view controller itself.

这篇关于在Swift中符合UITableViewDelegate和UITableViewDatasource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆