缺少返回UITableViewCell

我敢肯定这个问题已经问过了,但是找不到嵌套的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


        }

    }

}


蛊毒传说
浏览 719回答 3
3回答

慕森卡

在方法开始时声明单元格,根据部分和行号为单元格分配一个值,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。在Swift 1.2中,以这种方式初始化常量(let cell ...)的可能性是新的。另外,您可以创建一个单元格并在每种情况下“立即”返回: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()解决了“缺少期望的返回”编译器错误。如果在每种情况下创建了不同种类的单元(具有不同的类),则此模式很有用。

翻阅古今

方法中缺少return语句。返回语句示例。override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    if indexPath.section == 0{        switch (indexPath.row) {        case 0:            let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell        cell.backgroundColor = UIColor.redColor()        break        case 1:            let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell        cell.backgroundColor = UIColor.whiteColor()         break        default:            let cellId: NSString = "Cell"            var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell            break        }    }    if indexPath.section == 1{        switch (indexPath.row) {        case 0:            let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell        cell.backgroundColor = UIColor.redColor()        break        case 1:            let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell        cell.backgroundColor = UIColor.whiteColor()         break        default:             let cellId: NSString = "Cell"             var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell            break        }    }    return cell}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS