Swift:从ViewController中的tableView传递数据到另一个ViewController
问题描述:
我试图通过使用didSelectCellAtIndexPath和prepareForSegue方法将值从选定单元格传递到下一个视图控制器,但值不是通过。Swift:从ViewController中的tableView传递数据到另一个ViewController
代码都ViewControllers低于显示:
import UIKit
class MainMenu: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
// array of the menu options
let mainMenuOptions = ["Exercises 1", "Exercises 2", "Exercises 3"]
// UITableView
@IBOutlet weak var exerciseOptions: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
exerciseOptions.delegate = self
exerciseOptions.dataSource = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Table configuration
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ExercisesTableViewCells"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExercisesTableViewCell
cell.textLabel!.text = mainMenuOptions[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
// Segue to VC based on row selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0{
self.performSegueWithIdentifier("SegueToExercises", sender: self)
}
else if indexPath.row == 1{
self.performSegueWithIdentifier("SegueToExercises", sender: self)
}
else{
self.performSegueWithIdentifier("SegueToReminders", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SegueToExercise"){
let viewController = segue.destinationViewController as? ExerciseMenu
viewController!.mainMenuValue = exerciseOptions.indexPathForSelectedRow!.row
}
}
对于第二个视图控制器:
import UIKit
class ExerciseMenu: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var exerciseOptionsTitle: UILabel!
@IBOutlet weak var exerciseOptionsTable: UITableView!
@IBAction func beginWorkout(sender: AnyObject) {
}
// receives data from MainMenu
var mainMenuValue: Int!
override func viewDidLoad() {
super.viewDidLoad()
exerciseOptionsTable.delegate = self
exerciseOptionsTable.dataSource = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ExerciseMenuCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExercisesTableViewCell
cell.textLabel!.text = String(mainMenuValue)
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
答
见this线程这种现象可能的解释。 解决问题的第一步是在您的prepareForSegue方法中设置一个断点,以确定exerciseOptions.indexPathForSelectedRow
在该点上是否已为零,在这种情况下甚至发生了某种情况,即使发生了与值相关的错误(上面的链接可以提供帮助你诊断)。
尝试在您的表视图控制器中调用一个名为类似于selectedRow的属性,并在didSelectRowAtIndexPath
中设置self.selectedRow = indexPath.row
。这样你就有了自己的变量,比exerciseOptions.indexPathForSelectedRow
更可靠。然后适当地更改prepareForSegue
中的代码。所以给你完整的方法,因为我设想他们:
(作为第一视图控制器的一个属性)var selectedRow: Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0{
self.performSegueWithIdentifier("SegueToExercises", sender: self)
self.selectedRow = 0
}
else if indexPath.row == 1{
self.performSegueWithIdentifier("SegueToExercises", sender: self)
self.selectedRow = 1
}
else{
self.performSegueWithIdentifier("SegueToReminders", sender: self)
self.selectedRow = indexPath.row
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SegueToExercise"){
let viewController = segue.destinationViewController as? ExerciseMenu
viewController!.mainMenuValue = self.selectedRow!
}
}
免责声明:我还在学习斯威夫特,所以我就判断自选/可选的展开可能不是我不知道的技术原因,但我认为这里的整体理论是合理的。
谢谢您的回复。我应用了你的建议方法,在每个if语句之后添加一个print(selectedRow)。它们分别返回Optional(0),Optional(1)和Optional(2),但在viewDidLoad()中打印mainMenuValue时返回nil。有什么建议么? –
我还在prepareForSegue函数中添加了一个print(selectedRow)语句,它返回nil –
Hrmmm,我不确定。只是踢,如果你只是在第一个视图控制器的viewDidLoad中硬编码selectedRow = 1(或任何其他Int)会发生什么?当你准备好预测时,它仍然是零吗?哦,还试着明确地使selectedRow强大? –