缺少返回UITableViewCell

KML*_*KML 5 uitableview ios swift

我确定之前已经问过这个问题,但我找不到一个能够解决嵌套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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 9

  • 在方法的开头声明单元格,
  • 根据节和行号为单元格赋值,
  • 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
}
Run Code Online (Sandbox Code Playgroud)

fatalError()误差函数被标记为@noreturn,所以编译器知道程序的执行将不会从默认的情况下继续.(这也有助于在程序中找到逻辑错误.)

编译器将验证是否cell在所有其他情况下分配了值.

以这种方式初始化constant(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)")
    }
}
Run Code Online (Sandbox Code Playgroud)

再次,调用fatalError()解决了"缺少返回预期"编译器错误.

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