为什么Swift UITableViewController模板在tableView cellForRowAtIndexPath方法中使用选项参数? [英] Why does Swift UITableViewController template use optionals arguments in tableView cellForRowAtIndexPath method?

查看:102
本文介绍了为什么Swift UITableViewController模板在tableView cellForRowAtIndexPath方法中使用选项参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您创建新的UITableViewController类,您将看到覆盖的注释方法:

If you create new UITableViewController class, you will see commented method for override:

/*
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

    // Configure the cell...

    return cell
}
*/

您可以取消注释方法,因为错误而无法正常工作

You can uncomment method and it will not work because of error

'UITableView?' does not have a member named 'dequeueReusableCellWithIdentifier'

原因是:tableView被定义为可选类型 UITableView?,你必须在调用方法之前解包tableView。例如:

The reason is: tableView is defined as optional type "UITableView?" and you have to unwrap tableView before call the method. For example like this:

let cell = tableView!.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

但是我们可以让他们隐式解包的选项并使用没有的tableView!

But we can make them implicitly unwrapped optionals and use tableView without !

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    return cell
}

问题是:为什么xcode将它们定义为选项?是否有任何理由或优势与隐含的未解决的期权?我们可以肯定,这种方法总是得到非零值吗?

The question is: why does the xcode define them as optionals? Does it have any reason or advatages vs implicitly unwrapped optionals? Can be we sure, that this method always gets not-nill values?

我们还会有其他错误

Constant 'cell' inferred to have type 'AnyObject!', which may be unexpected
Type 'AnyObject!' cannot be implicitly downcast to 'UITableViewCell'; did you mean to use 'as' to force downcast?

我们可以通过添加UITableViewCell来修复它,如下所示:

We can fix it by adding as UITableViewCell to the end like this:

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell

我不知道为什么默认情况下模板看起来不像这样:

I have no idea why doesn't template look like this by default:

/*
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell //or your custom class

    // Configure the cell...

    return cell
}
*/


推荐答案

是的,这很奇怪。事实上,如果你删除模板提供的方法并开始键入每个方法,Xcode自动完成将建议使用隐式解包的可选参数的方法,例如

Yes, it's weird. In fact, if you erase the template provided methods and start typing each of them, Xcode autocompletion will suggest methods with implicitly unwrapped optional arguments such as

tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

我猜模板目前是错误的,可能会在以后修复,因为模板版本不是'甚至建议用手打字。如果你把它们留在那里,它们会被正常调用,并且只要你根据需要正确地解开参数它就会起作用。

I guess the templates are currently wrong and might be fixed later, given the fact the template versions aren't even suggested when typing by hand. If you leave them there, they do get called normally though, and it will work as long as you correctly unwrap the parameters as needed.

参见此问题了解有关讨论的更多信息。

See this question for more on the discussion.

这篇关于为什么Swift UITableViewController模板在tableView cellForRowAtIndexPath方法中使用选项参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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