亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

缺少返回UITableViewCell

缺少返回UITableViewCell

iOS
蠱毒傳說 2020-02-02 15:49:20
我敢肯定這個問題已經問過了,但是找不到嵌套的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        }    }}
查看完整描述

3 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

在方法開始時聲明單元格,

根據部分和行號為單元格分配一個值,

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()解決了“缺少期望的返回”編譯器錯誤。


如果在每種情況下創建了不同種類的單元(具有不同的類),則此模式很有用。


查看完整回答
反對 回復 2020-02-02
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

方法中缺少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

}



查看完整回答
反對 回復 2020-02-02
  • 3 回答
  • 0 關注
  • 793 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號