缺少返回 UITableViewCell [英] Missing return UITableViewCell
问题描述
我确定这个问题以前有人问过,但我找不到解决嵌套 if-else 和 switch-case 逻辑问题的答案.
我有一个 UITableView
有两个部分,每个部分都有两个自定义单元格.就是这样.4个细胞.但无论我做什么,我都会收到预期返回 UITableViewCell
的函数中缺少返回值"
I am sure this question have been asked before but I can't find an answer that solves my problem with nested if-else and switch-case logic.
I have a UITableView
with two sections, each sections has two custom cells. That is it it. 4 cells. But no matter what I do I get "Missing return in a function expected to return UITableViewCell
"
问题如何更改此设置,以便在底部获得满足快速逻辑的 else 语句?
Question How can I change this setup so that I get an else statement at the bottom that will satisfy swift logic?
非常感谢任何帮助
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0{
switch (indexPath.row) {
case 0:
let cell0: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell0.backgroundColor = UIColor.redColor()
break
case 1:
let cell1: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell1.backgroundColor = UIColor.whiteColor()
break
default:
break
}
}
if indexPath.section == 1{
switch (indexPath.row) {
case 0:
let cell10: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell10.backgroundColor = UIColor.redColor()
break
case 1:
let cell11: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell11.backgroundColor = UIColor.whiteColor()
break
default:
break
}
}
}
推荐答案
- 在方法开始处声明单元格,
- 根据节和行号为单元格赋值,
- 在所有不应该发生"的情况下抛出
fatalError()
, - 返回单元格.
另请注意,不需要 break
语句.默认的Swift 中的行为不会进入下一个案例.
Also note that the break
statements are not needed. The default
behavior in Swift is not to fall through to the next case.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SettingsCell
switch(indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
default:
fatalError("Unexpected row (indexPath.row) in section (indexPath.section)")
}
case 1:
switch (indexPath.row) {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
default:
fatalError("Unexpected row (indexPath.row) in section (indexPath.section)")
}
default:
fatalError("Unexpected section (indexPath.section)")
}
return cell
}
fatalError()
错误函数被标记为 @noreturn
,所以编译器知道程序执行不会从默认情况.(这也有助于发现程序中的逻辑错误.)
The fatalError()
error function is marked as @noreturn
, so the
compiler knows that program execution will not continue from the
default cases. (This also helps to find logic errors in the program.)
编译器验证一个值被分配给了cell
其他情况.
The compiler verifies that a value is assigned to cell
in all
other cases.
在此初始化常量(let cell ...
)的可能性方式是 Swift 1.2 中的新功能.
The possibility to initialize a constant (let cell ...
) in this
way is new in Swift 1.2.
或者,您可以创建一个单元格并立即"返回它在每种情况下:
Alternatively, you can create a cell and return it "immediately" in each case:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
return cell
default:
fatalError("Unexpected row (indexPath.row) in section (indexPath.section)")
}
case 1:
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
return cell
default:
fatalError("Unexpected row (indexPath.row) in section (indexPath.section)")
}
default:
fatalError("Unexpected section (indexPath.section)")
}
}
再次调用 fatalError()
解决了缺少预期返回"的编译器错误.
Again, calling fatalError()
solves the "missing return expected" compiler
error.
如果有不同种类的细胞,这种模式很有用(使用不同的类)在每种情况下创建.
This pattern can be useful if there are different kinds of cells (with different classes) created in each case.
这篇关于缺少返回 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!