gpt4 book ai didi

ios - 缺少返回 UITableViewCell

转载 作者:搜寻专家 更新时间:2023-10-30 22:28:40 25 4
gpt4 key购买 nike

我确信这个问题以前有人问过,但我找不到解决我的嵌套 if-else 和 switch-case 逻辑问题的答案。
我有一个包含两个部分的 UITableView,每个部分都有两个自定义单元格。就是这样。 4个细胞。但无论我做什么,我都会得到“预期返回 UITableViewCell 的函数中缺少返回值”

问题如何更改此设置,以便在底部获得满足快速逻辑的 else 语句?

非常感谢任何帮助

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 中的行为不会进入下一个案例。

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,所以编译器知道程序执行不会从默认情况下。 (这也有助于发现程序中的逻辑错误。)

编译器验证一个值被赋值给所有的cell其他情况。

初始化常量(let cell ...)的可能性方式在 Swift 1.2 中是新的。


或者,您可以创建一个单元格并“立即”返回它在每种情况下:

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() 解决了“missing return expected”编译器错误。

如果有不同种类的单元格,这种模式会很有用(具有不同的类)在每种情况下创建。

关于ios - 缺少返回 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com