Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 预期返回的函数中缺少返回';UITableViewCell_Ios_Swift_Uitableview_Switch Statement - Fatal编程技术网

Ios 预期返回的函数中缺少返回';UITableViewCell

Ios 预期返回的函数中缺少返回';UITableViewCell,ios,swift,uitableview,switch-statement,Ios,Swift,Uitableview,Switch Statement,为什么会出现这个错误? Switch语句不返回单元格? 有人能解释一下吗 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: let cell = tableView.dequeueReusableCell(withIdentifier: "LogoSty

为什么会出现这个错误? Switch语句不返回单元格? 有人能解释一下吗

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    switch indexPath.row {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "LogoStyle1", for: indexPath) as! LogoStyle1
        cell.backgroundColor = UIColor.clear
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: "FieldStyle1", for: indexPath) as! FieldStyle1
        cell.backgroundColor = UIColor.clear
        cell.delegate = self
        return cell
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonStyle1", for: indexPath) as! ButtonStyle1
        cell.backgroundColor = UIColor.clear
        cell.delegate = self
        return cell
    default:
        break
    }
}

这是编译器不再担心的事情之一。编译器基本上是在问您,如果
indexPath.row
既不是0,也不是1,也不是2怎么办?如果是那样的话,还什么。作为程序员,您知道
indexPath.row
将始终是这些值之一,但编译器不知道这一点

因此,您需要在
default
情况下返回一些内容,但返回任何内容都没有意义,是吗?在这种情况下,我会写:

fatalError("Somehow indexPath.row is an unexpected value.")

如果达到
默认值
情况,这将使应用程序崩溃。如果达到默认情况,则可能意味着代码有问题(或者API中有错误(不太可能)!),因为正如您所知,表视图只有3行。应用程序可能会在此时崩溃。

这是编译器不再担心的事情之一。编译器基本上是在问您,如果
indexPath.row
既不是0,也不是1,也不是2怎么办?如果是那样的话,还什么。作为程序员,您知道
indexPath.row
将始终是这些值之一,但编译器不知道这一点

因此,您需要在
default
情况下返回一些内容,但返回任何内容都没有意义,是吗?在这种情况下,我会写:

fatalError("Somehow indexPath.row is an unexpected value.")

如果达到
默认值
情况,这将使应用程序崩溃。如果达到默认情况,则可能意味着代码有问题(或者API中有错误(不太可能)!),因为正如您所知,表视图只有3行。应用程序此时可能会崩溃。

CellForRowAtIndexPath委托方法返回单元格,即使numberOfRowsInSection为0。所以在swicth的默认块中,您需要返回一个单元格。
并且错误将消失。

CellForRowAtIndexPath委托方法返回单元格,即使numberOfRowsInSection为0。所以在swicth的默认块中,您需要返回一个单元格。

错误将消失。

您需要在默认语句中返回单元格。默认值:返回UITableViewCell()。为什么需要此选项?@John,因为
cellForRowAt
func定义。@John,因为该方法必须返回
UITableViewCell
类的实例。您需要在默认语句中返回单元格。默认值:return UITableViewCell()为什么需要它?@John,因为
cellForRowAt
func定义。@John,因为该方法必须返回
UITableViewCell
类的实例。在本例中,我还需要删除break语句。İt是不必要的。@John在Swift中,你不需要在一个案例结束时打破
break
。只要写下fatalError,你就会没事的。惊人的解释。谢谢。@John另一个选项是删除案例2并将其代码移到默认值。顺便说一句,UIColor是冗余的<代码> CELL .BACKBASESTORK=。非常感谢。有人否决了这个问题,但它确实帮助了我。请你们把这个问题投上一票,这样其他人就可以理解了。我也需要删除这个例子中的break语句。İt是不必要的。@John在Swift中,你不需要在一个案例结束时打破
break
。只要写下fatalError,你就会没事的。惊人的解释。谢谢。@John另一个选项是删除案例2并将其代码移到默认值。顺便说一句,UIColor是冗余的<代码> CELL .BACKBASESTORK=。非常感谢。有人否决了这个问题,但它确实帮助了我。请你们把这个问题投上一票,这样其他人就可以理解了。