首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift 3 Segue Q!-原型单元到多个VCs使用didSelectRowAtIndexPath

Swift 3 Segue Q!-原型单元到多个VCs使用didSelectRowAtIndexPath
EN

Stack Overflow用户
提问于 2017-04-19 16:43:44
回答 1查看 165关注 0票数 1

我对Swift相对来说还是个新手--我为我的医院开发了一个应用程序(我是一名住院医生),这基本上是我用一系列tableViews构建的移动参考工具。

问题:我无法从我的原型单元格到多个VC.

  • 我最初的VC是一个tableVC,我使用嵌套数组将原型单元格构建成两个部分。在这里看到的
  • 目标VC也是TableVC,我希望每个tableCell的initialVC都能转换到不同的目标TableVC。
  • 我能够设置一些从我的initialTVC到destinationTVCs,我还嵌入了一个导航控制器-> 下面是我的故事板设置

我想要做的事是:

  1. 使用prepareForSegue函数设置我在故事板中给每个segue的segue ID
  2. 使用didSelectRowAtIndexPath,与一些If语句一起,指定要执行哪个单元格,以执行segue。
  3. 保持这个故事板导航控制器的位置-喜欢它提供的功能和简单的实现,考虑到我将在进入我的detailView页面之前,再添加一个级别的这些控件作为“子菜单”。

这里是我的Swift 3代码:

代码语言:javascript
运行
复制
//  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"}

        }
}

我成功地建造了这座房子,但是我的船还在工作。我几乎宁愿仍然有一些错误代码来处理!如果有人有任何有用的建议/评论/链接供进一步阅读,会非常感谢。已经阅读了大量的其他页面和文档,试图让这个工作像我想要的那样简单,但到目前为止还没有骰子。谢谢你能提供的任何帮助!

更新4/20/17:我编辑了prepareForSegue函数,甚至在其中添加了一个"VCName“变量,这样我就可以调用segue.destination (即使"VCName”没有使用)--仍然没有给我一个segue (没有错误消息,它成功的构建),也不知道下一步该尝试什么。任何建议都要感谢!

在4/26/17上的更新:我使用了一些断点,发现准备和执行segue函数都没有触发。我重新构建并确保它是超类的重写函数(不知道为什么它不让我先动手),它成功了!特别感谢萨桑的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-19 17:06:53

据我所知,您的代码看起来很好,并且应该可以处理这个小的更改。记住,节和行是从0开始的索引。你有以下几点:

代码语言:javascript
运行
复制
if (indexPath.section == 1) && (indexPath.row == 1) {
  self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
}

我怀疑您正在点击第一节中的第一项而没有看到任何转换,但是上面的代码实际上是指第二节中的第二项。“联邦计划”小组。将代码更改为下面的代码,使其能够点击“儿童和孕妇”单元格以保存到下一个ViewController。

代码语言:javascript
运行
复制
if (indexPath.section == 0) && (indexPath.row == 0) {
   self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
}

此外,我建议您保存一些代码,方法是将所有的ViewController StoryBoard Id添加到类似的数组结构中,作为主标题数组,这样就可以将didSelectRowAtIndexPath函数中的许多if-StoryBoard语句更改为:

代码语言:javascript
运行
复制
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegue(withIdentifier: identifierList[indexPath.section][indexPath.row], sender: indexPath);
}

更新

关于最新问题的补充说明。prepareForSegue函数只是为了在呈现下一个场景之前先完成它的阶段。例如,如果CPW viewController中有一个"name“字段,您可以像这样在函数中设置它,然后再显示它:

代码语言:javascript
运行
复制
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "CPWSegue" { 
     let nextView = segue.destination as? CPWView
     nextView.name = "SOME NAME"
  }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43501532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档