gpt4 book ai didi

ios - 缺少返回 UITableViewCell

转载 作者:行者123 更新时间:2023-11-30 13:53:54 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 ...)way 是 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() 解决了“缺少预期返回”编译器的问题错误。

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

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

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