[英]Swift: Passing data from a tableView in a ViewController to another ViewController
I'm trying to pass the indexPath.row data from the selected cell to the next view controller using both the didSelectCellAtIndexPath and the prepareForSegue methods but the value isnt passing through. 我正在尝试使用didSelectCellAtIndexPath和prepareForSegue方法将indexPath.row数据从选定的单元格传递到下一个视图控制器,但该值未传递。
Code for both ViewControllers is show below: 这两个ViewController的代码如下所示:
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
}
}
For the Second View Controller: 对于第二视图控制器:
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
}
See this thread for possible explanations for this behavior. 见这个线程这种现象可能的解释。 The first step of troubleshooting would be to put a breakpoint in your prepareForSegue method to see if
exerciseOptions.indexPathForSelectedRow
is already nil at that point, in which case something is happening before the segue even occurs that's messing with the value (which the link above can help you diagnose). 故障排除的第一步是在您的prepareForSegue方法中放置一个断点,以查看
exerciseOptions.indexPathForSelectedRow
在那时是否已经为零,在这种情况下,甚至在segue发生之前就已经发生了一些与该值混淆的事情(上面的链接可以帮助您诊断)。
Try making a property in your table view controller called something like selectedRow, and in didSelectRowAtIndexPath
, set self.selectedRow = indexPath.row
. 尝试在表视图控制器中创建一个名为selectedRow之类的属性,并在
didSelectRowAtIndexPath
中设置self.selectedRow = indexPath.row
。 This way you have your own variable that'll be more reliable than exerciseOptions.indexPathForSelectedRow
. 这样,您将拥有自己的变量,该变量将比
exerciseOptions.indexPathForSelectedRow
更可靠。 Then change the code in prepareForSegue
appropriately. 然后适当地更改
prepareForSegue
的代码。 So to give you the full methods as I'm envisioning them: 因此,为您提供我所设想的完整方法:
(As a property of the first view controller) var selectedRow: Int?
(作为第一个视图控制器的属性)
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!
}
}
Disclaimer: I'm still learning Swift, so my judgment regarding optionals/optional unwrapping might be unideal for a technical reason I'm not aware of, but the overall theory here is sound, I think. 免责声明:我仍在学习Swift,所以我对可选项/可选解包的判断可能出于我不知道的技术原因而不理想,但我认为这里的总体理论是合理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.