缺少返回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()
在所有不应该发生的情况下, - 返回单元格。
- Declare the cell at the start of the method,
- assign a value to the cell depending on section and row number,
- throw a
fatalError()
in all cases that "should not occur", - return the cell.
另请注意,不需要 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.)
编译器验证是否已将值分配给所有
。
其他情况下的单元格
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屋!