2016-02-13 138 views
1

我試圖通過使用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 
} 

回答

0

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! 


    } 
} 

免責聲明:我還在學習斯威夫特,所以我就判斷自選/可選的展開可能不是我不知道的技術原因,但我認爲這裏的整體理論是合理的。

+0

謝謝您的回覆。我應用了你的建議方法,在每個if語句之後添加一個print(selectedRow)。它們分別返回Optional(0),Optional(1)和Optional(2),但在viewDidLoad()中打印mainMenuValue時返回nil。有什麼建議麼? –

+0

我還在prepareForSegue函數中添加了一個print(selectedRow)語句,它返回nil –

+0

Hrmmm,我不確定。只是踢,如果你只是在第一個視圖控制器的viewDidLoad中硬編碼selectedRow = 1(或任何其他Int)會發生什麼?當你準備好預測時,它仍然是零嗎?哦,還試着明確地使selectedRow強大? –

相關問題