Swift 3 Segue Q!- 多个 VC 的原型单元格 - 使用 didSelectRowAtIndexPath

Swift 3 Segue Q!- Prototype Cell to Multiple VCs - Using didSelectRowAtIndexPath

我对 Swift 比较陌生 - 正在为我的医院(我是一名住院医师)开发一个应用程序,它本质上是一个移动参考工具,我正在使用一系列 tableViews 构建它。

问题:我无法让我的 Segues 从我的原型单元格工作到多个 VCs。

我想为 Segues 做的事情:

  1. 设置 segue ID 我使用 prepareForSegue 函数

  2. 使用 didSelectRowAtIndexPath,使用一些 If 语句指定我想在哪个单元格中执行哪个转场,以执行转场。

  3. 保留 Storyboard Navigation 控制器 - 喜欢它提供的功能和简单的实现,考虑到我将在进入我的 detailView 之前添加更多级别的这些 segues 作为 "submenus"页数。

这是我的 Swift 3 代码:

//  MainTableViewController.swift

import UIKit

class MainTableViewController: UITableViewController {

    var MainTitleArray = [["Children and Pregnant Women", "Mental Health and Addiction", "Hunger and Food Insecurity", "Legal Aid", "Housing, Heat, and Emergency Assistance", "New Immigrants, Refugees, and the Underserved", "The Uninsured and the Underinsured"], ["Primers and the Private Sector", "Federal Programs", "Social Securty"]]
    var SubTitleArray = [["Resources for the most vulnerable in the family", "The programs to help those who need it", "When every night is a worry about food on the table", "How to defend yourself in a complicated system", "HEalth follows you into the home", "Your patient needs help - can they sign up worry-free?", "What to do when your options are limited"], ["What you need to know","Medicare Medicaid - What are they?","Disability and Insurance"]]



    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 90           
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {

       return MainTitleArray.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return MainTitleArray[section].count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell

     cell.MainTitleLabel.text = MainTitleArray[indexPath.section][indexPath.row]

     cell.MainSubTitleLabel.text = SubTitleArray[indexPath.section][indexPath.row]

     return cell}


  override  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if (indexPath.section == 0) && (indexPath.row == 0) {
            self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
        }
        else { self.performSegue(withIdentifier: "MHASegue", sender: indexPath)

        }}

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "CPWSegue" {
            let destVC = segue.destination as! CPWTableViewController
            destVC.VCName = "CPW"}

        }
}

我成功地构建了它,但是 segues 仍然无法正常工作。我几乎宁愿仍然有一些错误代码来处理一些事情!如果有人对进一步阅读有任何帮助 suggestions/comments/links,HUGELY 将不胜感激。已经阅读了大量的其他页面和文档,试图让它像我想要的那样简单地工作,但到目前为止还没有成功。提前感谢您提供的任何帮助!

2017 年 4 月 20 日更新:我已经编辑了 prepareForSegue 函数,甚至放入了一个 "VCName" 变量,这样我就可以调用 segue.destination (即使 "VCName" 使用)- STILL 没有给我一个 segue(没有错误消息,并且它成功构建)不确定接下来要尝试什么。任何建议表示赞赏!

更新于 2017 年 4 月 26 日:我使用了一些断点,发现准备和执行 segue 函数都没有触发。我重建并确保它是超类的 override 函数(不知道为什么它不让我事先这样做),并且它起作用了!特别感谢Sasaang的帮助!

据我所知,您的代码看起来不错,它应该适用于这个小改动。请记住,部分和行是从 0 开始的索引。您有以下内容:

if (indexPath.section == 1) && (indexPath.row == 1) {
  self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
}

我怀疑您正在点击第一部分中的第一项,但没有看到任何过渡,但上面的代码实际上是指第二部分中的第二项。 "Federal Programs" 单元格。将代码更改为以下内容,以便能够点击 "Children and Pregnant Women" 单元格以转到下一个 ViewController.

if (indexPath.section == 0) && (indexPath.row == 0) {
   self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
}

此外,我建议您通过将所有 ViewController 故事板 ID 添加到与主标题数组类似的数组结构中来保存一些代码,这样可以代替 didSelectRowAtIndexPath 中的许多 if-else 语句功能,您可以将其简单地更改为:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegue(withIdentifier: identifierList[indexPath.section][indexPath.row], sender: indexPath);
}

更新

关于更新问题的补充说明。 prepareForSegue 函数只是用来在呈现之前准备下一个场景。例如,如果 CPW viewController 中有一个 "name" 字段,您可以在显示它之前在函数中设置它:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "CPWSegue" { 
     let nextView = segue.destination as? CPWView
     nextView.name = "SOME NAME"
  }
}

您不要再在那里调用 performSegue(),它已经发生了。