As indicated in the previous answer for a similar question , -tableView:cellForRowAtIndexPath: should return a UITableViewCell . Therefore, you cannot return nil . However, I would also recommend not returning the following codes at the end of -tableView:cellForRowAtIndexPath: if you use if else or switch in it:
//bad code design var cell: UITableViewCell! return cell
or
//bad code design var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell return cell
You can do better code than create an instance of UITableViewCell that is never called just to turn off Xcode warnings!
So what would be the solution?
The main thing is that your last possible value for the if else statement is set to else (not to else if ). It is equally important to note that your last possible value for the switch statement is set to default: (not in case XXX: .
So your code should look like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell /* ... */ return cell } else if indexPath.row == 1 { let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne /* ... */ return cell } else { //set your last indexPath.row case in "else", not in "else if indexPath.row == 2"!!! switch segment { case 0: let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo /* ... */ return cell case 1: let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree /* ... */ return cell default: //set your last segment case in "default:", not in "case 2:"!!! let cell = tableView.dequeueReusableCellWithIdentifier("CellFour", forIndexPath: indexPath) as CellFour /* ... */ return cell } } //No need for a fictive "return cell" with this code!!! }
If segment is not optional, thanks to tuples you can even reduce the previous code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { switch (indexPath.row, segment) { case (0, _): let cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell return cell case (1, _): let cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne return cell case (2, 0): let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo return cell case (2, 1): let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree return cell default: