【问题标题】:Performsegue within tableviewcell not working在 tableviewcell 中执行segue 不起作用
【发布时间】:2020-04-04 00:33:19
【问题描述】:

我的视图控制器中有一个 tableview,当我单击我的自定义 tableview 单元格时,我在执行 push segue 时遇到问题。当我的 tableview 在视图控制器中时,我必须做一些独特的事情吗?代码如下。

extension MyPersonalViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showGuest", sender: self)
    tableView.deselectRow(at: indexPath, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showGuest" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let guestVC = segue.destination as! GuestViewController
            guestVC.navigationItem.title = "Project"  
        }
    }
}

【问题讨论】:

  • 抱歉,打错了。它显示 showGuest 无处不在。
  • “不工作”意味着它没有推送到下一个视图?你确定标识符是“showGuest”吗?

标签: swift uitableview uiviewcontroller segue swift5


【解决方案1】:
#You can try the below code:#

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SegueIdentifierName" {
            let destinationVC = segue.destination as! DestinationViewController
            destinationVC.Outlet = "label"  
    }
}

【讨论】:

    【解决方案2】:

    在情节提要中,您应该能够通过按住控制键,然后单击单元格,然后拖动到目标视图控制器来创建转场。

    在您的didSelectRowAt 方法中,您无需执行任何操作。我的意思是你不需要打电话给performSegue。多亏了您在故事板中定义的 segue,所有这些都将在幕后发生。

    在您的 prepare 方法中,您需要执行以下操作:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "showGuest" else { return }
        guard let cell = sender as? UITableViewCell else { return }
        guard let indexPath = tableView.indexPath(for: cell) else { return }
        guard let dest = segue.destination as? GuestViewController else { return }
        dest.data = data[indexPath.row]
    }
    

    【讨论】:

      【解决方案3】:

      将表格单元所在的视图嵌入到 UINavgationController 中。

      实例化你的视图

       let guestVC = segue.destination as! GuestViewController
       guestVC.navigationItem.title = "Project"  
      
       self?.navigationController?.pushViewController(guestVC!, animated: true)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        相关资源
        最近更新 更多