ios - Missing return UITableViewCell -
i sure question have been asked before can't find answer solves problem nested if-else , switch-case logic.
have uitableview
2 sections, each sections has 2 custom cells. it. 4 cells. no matter "missing return in function expected return uitableviewcell
"
question how can change setup else statement @ bottom satisfy swift logic?
any appreciated
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 } } }
- declare cell @ start of method,
- assign value cell depending on section , row number,
- throw
fatalerror()
in cases "should not occur", - return cell.
also note break
statements not needed. default behavior in swift not fall through next case.
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 }
the fatalerror()
error function marked @noreturn
, compiler knows program execution not continue default cases. (this helps find logic errors in program.)
the compiler verifies value assigned cell
in other cases.
the possibility initialize constant (let cell ...
) in way new in swift 1.2.
alternatively, can create cell , return "immediately" in each case:
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)") } }
again, calling fatalerror()
solves "missing return expected" compiler error.
this pattern can useful if there different kinds of cells (with different classes) created in each case.
Comments
Post a Comment