我有兩個viewControllers.First一個是tableViewController,第二個是一個webViewController.I想要使用segue從1st vc傳遞數據到2nd vc。由於這個原因,我希望從「func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)」中的「chosenCellIndex」的值到prepareForSegue方法。但我無法從prepareForSegue方法訪問selectedCellIndex值。請幫忙。如何從prepareForSegue方法在同一類內訪問override func tableView屬性
類NewsTableViewController:的UITableViewController {
@IBOutlet var Alphacell: UITableView!
var chosenCellIndex = 0
//var temp = 0
var newspaper = ["A","B","C"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newspaper.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Alphacell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = newspaper[indexPath.row]
// Alphacell.tag = indexPath.row
//Alphacell.addTarget(self, action: "buttonClicked:",forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
chosenCellIndex = indexPath.row
//println(chosenCellIndex)
// Start segue with index of cell clicked
//self.performSegueWithIdentifier("Alphacell", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondViewController = segue.destinationViewController as WebViewController
secondViewController.receivedCellIndex = chosenCellIndex
println(chosenCellIndex)
}
}
我只是需要從FUNC的tableView到FUNC prepareForSegue取「chosenCellIndex」變量的值下面的代碼。但爲什麼func tableView和func prepareForSegue中「chosenCellIndex」的值不一樣。它是我的問題。 – Alex