ios 为什么 Swift UITableViewController 模板在 tableView cellForRowAtIndexPath 方法中使用可选参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24078503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Swift UITableViewController template use optionals arguments in tableView cellForRowAtIndexPath method?
提问by John Kakon
If you create new UITableViewController class, you will see commented method for override:
如果您创建新的 UITableViewController 类,您将看到覆盖的注释方法:
/*
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'
The reason is: tableView is defined as optional type "UITableView?" and you have to unwrap tableView before call the method. For example like this:
原因是:tableView 被定义为可选类型“ UITableView?”,并且您必须在调用该方法之前解包 tableView。例如像这样:
let cell = tableView!.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
But we can make them implicitly unwrapped optionalsand use tableView without !
但是我们可以让它们隐式解包的选项并使用 tableView 而不使用!
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
return cell
}
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?
问题是:为什么 xcode 将它们定义为可选项?与隐式解包的选项相比,它有什么理由或优势吗?我们可以确定,这个方法总是得到非零值吗?
Also we will have another errors
我们还会有另一个错误
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?
We can fix it by adding as UITableViewCell to the end like this:
我们可以通过在末尾添加 UITableViewCell 来修复它,如下所示:
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
}
*/
采纳答案by Cezar
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
是的,这很奇怪。事实上,如果你删除模板提供的方法并开始输入它们中的每一个,Xcode 自动完成将建议带有隐式解包的可选参数的方法,例如
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 questionfor more on the discussion.
回答by Ryan
Actually this is the correct way to use the delegate method.
实际上这是使用委托方法的正确方法。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CELL_ID", forIndexPath: indexPath)
cell.textLabel.text = "aaaa"
return cell
}