简体   繁体   English

Swift中具有两个不同序列的一个原型单元

[英]One Prototype Cell with Two Different Segues in Swift

I am trying to connect one prototype cell to two different segue pathways that each lead to another view controller. 我正在尝试将一个原型单元连接到两个不同的segue路径,每个路径都通向另一个视图控制器。 I read in another posts that hooking up the segues has to be done from the source view controller itself to the destination view controller, not the prototype cell to the view controller. 我在另一篇文章中读到,必须从源视图控制器本身到目标视图控制器,而不是原型单元到视图控制器的连接,才能完成。

My source view controller is called BuildsViewController, which is populated with tableview cells. 我的源视图控制器称为BuildsViewController,其中填充了表格视图单元格。 I am working on an app where clicking the cell will open up the destination view controller based on whether the login or guest button was clicked at the root view controller. 我正在开发一个应用程序,在该应用程序中,单击单元格将根据在根视图控制器上单击了登录名或来宾按钮来打开目标视图控制器。 (buttonClicked is an instance variable that stores the button that the user clicked on the root view controller). (buttonClicked是一个实例变量,用于存储用户在根视图控制器上单击的按钮)。

When I click on the cell on BuildsViewController, however, nothing happens. 但是,当我单击BuildsViewController上的单元格时,什么也没有发生。 Where did I go wrong? 我哪里做错了? Note that the login button has tag 0 and the guest button has tag 1. 请注意,登录按钮的标签为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

}

Thank you! 谢谢!

Swift 3 modified the signature of didSelectRowAtIndexPath method. Swift 3修改了didSelectRowAtIndexPath方法的签名。 You need to add override to the method name to override the superclass method. 您需要在方法名称上添加重写以覆盖超类方法。

Replace: 更换:

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM