Swift:将数据从ViewController中的tableView传递到另一个ViewController

时间:2016-02-13 15:26:53

标签: ios uitableview uiviewcontroller

我尝试使用didSelectCellAtIndexPath和prepareForSegue方法将indexPath.row数据从选定的单元格传递到下一个视图控制器,但是值没有通过。

两个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


    }

}

1 个答案:

答案 0 :(得分:0)

请参阅this主题以获取此行为的可能解释。 故障排除的第一步是在prepareForSegue方法中放置一个断点,以查看此时exerciseOptions.indexPathForSelectedRow是否已经为零,在这种情况下,在segue发生之前发生的事情正在发生,这会使值变得混乱(链接以上可以帮助你诊断)。

尝试在表视图控制器中创建一个名为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!


  }
}

免责声明:我还在学习Swift,所以我对选项/可选展开的判断可能是我不了解的技术原因,但我认为这里的整体理论是合理的。