【问题标题】:Swift 3 Segue Q!- Prototype Cell to Multiple VCs - Using didSelectRowAtIndexPathSwift 3 Segue Q!- 多个 VC 的原型单元 - 使用 didSelectRowAtIndexPath
【发布时间】:2017-04-19 16:43:44
【问题描述】:

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

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

  • 我的初始 VC 是一个 tableVC,我使用嵌套数组将原型单元构建成两个部分。 Seen here
  • 目标 VC 也是 TableVC,我希望初始 VC 的每个 tableCell 都连接到不同的目标 TableVC。
  • 我能够设置一些从初始TVC 到destinationTVC 的Segue,我还嵌入了一个导航控制器--> Here's what my storyboard setup looks like

我想为 Segues 做些什么:

  1. 设置 segue ID,我使用 prepareForSegue 函数在情节提要中为每个 segue 提供

  2. 使用 didSelectRowAtIndexPath 和一些 If 语句指定我要执行哪个单元格,执行哪个 segue。

  3. 保留 Storyboard Navigation 控制器 - 喜欢它提供的功能和简单的实现,考虑到我将在进入我的 detailView 页面之前将这些 segue 的多级添加为“子菜单”。

这是我的 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 仍然无法正常工作。我几乎宁愿仍然有一些错误代码来处理一些事情!如果有人有任何有用的建议/cmets/链接以供进一步阅读,非常将不胜感激。已经阅读了大量的其他页面和文档,试图让它像我想要的那样简单地工作,但到目前为止还没有骰子。提前感谢您提供的任何帮助!

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

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

【问题讨论】:

  • 您能否尝试在代码中添加几个断点,一个在 self.preformSegue 调用处,另一个在 prepare for segue 函数中的某个位置,并检查当您点击时是否触发其中一个或两个正确的单元格。
  • 尝试了断点,由于某种原因,当我点击任何单元格时,准备和执行功能都不会触发。 - - 也许我需要重新配置我的 didSelectRowAtIndexPath if else 语句的格式?
  • 您能否更新您的问题以包含整个班级的代码。如果我能看到整个事情可能会有所帮助。
  • 已更新以包含完整代码 - 我是否需要在我的代码中使用 segue 进行一些 IB 引用?
  • 好的,你说它没有在 performSegue 调用中达到断点。你能检查一下它是否完全达到了这个功能。在检查节和行的 if 语句之前;添加以下打印语句并将断点设置到这一行: print(indexPath.section, indexPath.row) ,这样你也可以在控制台中看到索引的值是什么。如果功能设置正确,则应始终触发此行。

标签: swift uinavigationcontroller ios6 segue didselectrowatindexpath


【解决方案1】:

据我所知,您的代码看起来不错,它应该可以与这个微小的更改一起使用。请记住,节和行是从 0 开始的索引。您有以下内容:

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

我怀疑您正在点击第一部分中的第一项并且没有看到任何转换,但上面的代码实际上是指第二部分中的第二项。 “联邦计划”单元格。将代码更改为以下内容,以便能够点击“儿童和孕妇”单元格以转到下一个 ViewController。

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

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

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

更新

关于更新问题的附加说明。 prepareForSegue 函数只是用来在呈现下一个场景之前暂存它。例如,如果 CPW 视图控制器中有一个“名称”字段,您可以在呈现它之前在函数中设置它:

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

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

【讨论】:

  • 关于使用故事板标识符数组的好主意,一些更智能的代码! - 出于某种原因,它仍然没有执行segues。我最初将索引路径设置为 0.0,但更改了几次以查看是否是问题所在。我已经按照您的建议重置了它,但仍然无法正常工作。我想知道在 prepareForSegue 中是否需要一些不同的设置?或者这种类型的程序化 segue 会干扰我的故事板 segue/Nav 控制器设置?有什么想法吗?
  • 我明白了,那么故事板中的 segue 属性是什么样的?
  • 标识符:CPWSegue,我没有指定或创建不同的 UIStoryBoardSegue 类,它是一个 Show/Push。它从最初的 TVC 绘制到两个不同的 TVC,在 viewController 到 viewController 级别(而不是原型单元到 viewController)。 - - 几年前有人在有关此主题的帖子中建议绘制 sb segues 并将 TVC 设置为静态单元,然后将其切换回原型,但试图保持 segues 到位。这对我不起作用,但也许值得更多探索?今天早上我什至从头开始重建,但没有运气。再次感谢您的帮助,顺便说一句。
  • 你也有 prepareForSegue 函数吗?如果不能,您可以创建一个断点并在那里放置一个断点,以查看当您点击第一个单元格时它是否命中。试图确定断开连接的位置。
  • 好点 - 我有一个 prepareForSegue 函数,但我发现它不在上面的代码中,所以我编辑了上面的帖子以包含它。仍然没有工作 - 您是否发现我的 prepareForSegue 代码有任何问题?
猜你喜欢
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2017-03-02
  • 2014-09-28
相关资源
最近更新 更多