我如何在 swift 3 的一个视图控制器中填充两个不同的表视图

ahm*_*med 5 uitableview ios swift

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

    if tableView == table1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! acntTableViewCell
        cell.account.text = account[indexPath.row].email
        return cell
    }
    else if tableView == table2 {
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2")
            as! popTableViewCell

        cell2.pop.text = pop[indexPath.row].answer
        return cell2
    }
}//its give me  error here Missing return  in function
Run Code Online (Sandbox Code Playgroud)

我将在一个视图控制器中填充两个不同的表。当我返回单元格时,它给我错误我做错的函数中缺少返回值,任何人都可以告诉我这段代码有什么问题

Sul*_*han 6

首先,您应该使用===(references) 而不是==.

这是断言失败是告诉编译器不存在其他函数方式的好方法的情况之一,例如:

if tableView === table1 {
    return ...
} else if tableView === table2 {
    return ...
} else {
    fatalError("Invalid table")
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用switch

switch tableView {
   case table1:
       return ...
   case table2:
       return ...
   default:
       fatalError("Invalid table")
}
Run Code Online (Sandbox Code Playgroud)


Phi*_*lls 0

编译器正在分析如果tableView既不是table1也不是的话会发生什么table2。如果发生这种情况,该函数将退出且不返回任何内容。

这是一个错误。