我对Swift相对来说还是个新手--我为我的医院开发了一个应用程序(我是一名住院医生),这基本上是我用一系列tableViews构建的移动参考工具。
问题:我无法从我的原型单元格到多个VC.
我想要做的事是:
这里是我的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"}
}
}
我成功地建造了这座房子,但是我的船还在工作。我几乎宁愿仍然有一些错误代码来处理!如果有人有任何有用的建议/评论/链接供进一步阅读,会非常感谢。已经阅读了大量的其他页面和文档,试图让这个工作像我想要的那样简单,但到目前为止还没有骰子。谢谢你能提供的任何帮助!
更新4/20/17:我编辑了prepareForSegue函数,甚至在其中添加了一个"VCName“变量,这样我就可以调用segue.destination (即使"VCName”没有使用)--仍然没有给我一个segue (没有错误消息,它成功的构建),也不知道下一步该尝试什么。任何建议都要感谢!
在4/26/17上的更新:我使用了一些断点,发现准备和执行segue函数都没有触发。我重新构建并确保它是超类的重写函数(不知道为什么它不让我先动手),它成功了!特别感谢萨桑的帮助!
发布于 2017-04-19 17:06:53
据我所知,您的代码看起来很好,并且应该可以处理这个小的更改。记住,节和行是从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-StoryBoard语句更改为:
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(),它已经发生了。
https://stackoverflow.com/questions/43501532
复制相似问题