【问题标题】:One Prototype Cell with Two Different Segues in Swift在 Swift 中具有两种不同 Segue 的一个原型单元
【发布时间】:2017-08-31 02:51:33
【问题描述】:

我正在尝试将一个原型单元连接到两个不同的 segue 路径,每个路径都通向另一个视图控制器。我在另一篇文章中读到,连接segues 必须从源视图控制器本身到目标视图控制器,而不是原型单元到视图控制器。

我的源视图控制器称为 BuildsViewController,其中填充了 tableview 单元格。我正在开发一个应用程序,其中单击单元格将打开目标视图控制器,具体取决于在根视图控制器上是否单击了登录或访客按钮。 (buttonClicked 是一个实例变量,用于存储用户在根视图控制器上单击的按钮)。

但是,当我单击 BuildsViewController 上的单元格时,什么也没有发生。我哪里做错了?请注意,登录按钮的标签为 0,访客按钮的标签为 1。

     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch self.buttonClicked?.tag {
    case 0?:
            performSegue(withIdentifier: "showLogin", sender: self)
            break;
    case 1?:
            performSegue(withIdentifier: "showGuest", sender: self)
            break;

    default:
        preconditionFailure()
    }
}



   override func prepare (for segue: UIStoryboardSegue, sender: Any?) {


    switch segue.identifier {
    case "showLogin"?:

       if let row = self.tableView.indexPathForSelectedRow?.row {

            let build = self.buildStore.allBuilds [row]
            let detailBuildsViewController = segue.destination as! BuildDetailViewController
                detailBuildsViewController.build = build
            break;
        }
    case "showGuest"?:


        if let row = self.tableView.indexPathForSelectedRow?.row {

            let build = self.buildStore.allBuilds [row]
                let detailBuildsViewControllerGuest = segue.destination as! BuildDetailViewControllerGuest
                detailBuildsViewControllerGuest.build = build
            break;
        }
    default:
        preconditionFailure("Unexpected segue identifier")
        }
    }
    override func tableView (_ tableView: UITableView,
                         cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
    let build = buildStore.allBuilds [indexPath.row]
    cell.textLabel?.text = build.name

    return cell

}

谢谢!

【问题讨论】:

  • 显示您的cellForRowAt 代码
  • 你的 didselect 方法被调用了吗??
  • 哦,没错。我需要重写 didselect 方法并且方法签名是错误的。我现在修好了。它工作得很好!谢谢! @luckyShubhra
  • 欢迎您@S.Patel。所以准备提供帮助:)

标签: ios swift uitableview uiviewcontroller segue


【解决方案1】:

Swift 3 修改了 didSelectRowAtIndexPath 方法的签名。您需要在方法名称中添加 override 以覆盖超类方法。

替换:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多