ios - 无法在 Swift 中将类型 'UITableViewCell' 的值转换为 'NSIndexPath'

标签 ios swift uitableview segue

我在 tableview 中显示数据,我在它的 TableViewCell 上添加了一个 show segue 单击我以移动到 DetailsController 并传递单元格的数据单击 IndexPath 到参数中的另一个 Controller 。 这是 segue 代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "toDetails", sender: indexPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let indexPath = sender as! IndexPath
    let indice = indexPath.row as NSInteger
    let nomCandidat = "candidats"

    let detailsViewController = segue.destination as! DetailsViewController
    detailsViewController.idtrip = 10
}

我确保 segue 标识符是正确的。

错误:

Could not cast value of type UITableViewCell to NSIndexPath

最佳答案

您不能将 indexPath 作为 sender 参数发送。 sender: 是触发 segue 的 UI 元素,在本例中是 UITableViewCell。 因此,您在发件人中发送 tableViewCell 实例,例如:

guard let cell = tableView.cellForRowAt(indexPath: indexPath) else {return}
//alternatively 
//let cell = tableView.cellForRowAt(indexPath: indexPath)! 
performSegue(withIdentifier: "toDetails", sender: cell)

关于ios - 无法在 Swift 中将类型 'UITableViewCell' 的值转换为 'NSIndexPath',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384734/

相关文章:

ios - 无法在 neSTLed CollectionView 中获取 TableViewCell 的 indexPath

ios - 从头开始重写 iOS 应用程序 "Could not change executable permissions on the application."

ios - RestKit、CoreData 和 Swift - 我似乎无法取回结果

ios - 将 Game Center 好友及其分数加载到 UITableView

ios - 在 NavBar 上使用 Back 后,RefreshControl 会导致 TableView 的第一个 Cell 出错

ios - Swift - 无法将函数转换为泛型

c# - 无法通过 Alamofire.upload multipartFormData 调用 Web Api

arrays - Xcode 10.2 中下标的使用不明确

swift - 以编程方式从主线程以外的其他地方选择 UITableView 行

ios - UITableViewCell显示问题