无法将“IndexPath.Type”类型的值转换为预期的参数类型“IndexPath”

H.E*_*ein 0 uitableview ios swift

我正在尝试使用表视图并在 func 中: cellForRowAt我收到一个错误:Cannot convert value of type 'IndexPath.Type' to expected argument type 'IndexPath'

我的代码是:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellDish:DishTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellDish", for: IndexPath) as! DishTableViewCell      
    return cellDish
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*ith 5

问题是您的 IndexPath 参数。

您使用的是类而不是实例。正确的方法是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellDish:DishTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellDish", for: indexPath) as! DishTableViewCell      
    return cellDish
}
Run Code Online (Sandbox Code Playgroud)