2016-04-21 40 views
1

我有兩個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) 
} 

}

+0

我只是需要從FUNC的tableView到FUNC prepareForSegue取「chosenCellIndex」變量的值下面的代碼。但爲什麼func tableView和func prepareForSegue中「chosenCellIndex」的值不一樣。它是我的問題。 – Alex

回答

0

確保您已設置在故事板SEGUE標識符然後執行:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "someIdentifier"{ 
     let secondViewController = segue.destinationViewController as? WebViewController 
//Also, make sure this is the name of the ViewController you are perfoming the segue to. 
     secondViewController.receivedCellIndex = chosenCellIndex 
     print(chosenCellIndex) 
    } 
    } 

此外,什麼版本的Xcode和Swift你在用嗎? println()現在只是print()。要做到這一點

0

最好的辦法是從做選擇行功能,即例如

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     let objects = newspaper[indexPath.row] 
     let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController 
     vc.someString = objects.title 
     self.presentViewController(vc, animated: true, completion: nil) 

} 

和prepareForSegue使用此代碼:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let path = self.tableView.indexPathForSelectedRow()! 
    segue.destinationViewController.detail = self.detailForIndexPath(path) 
} 
+0

我只需要從func tableView的func prepareForSegue中獲取「chosenCellIndex」變量的值。但爲什麼func tableView和func prepareForSegue中「chosenCellIndex」的值不一樣。它是我的問題。 – Alex

1

,以獲得選擇你需要得到indexpath細胞指數對於選定的行,則需要從indexpath獲取段或行索引。

見得到行索引

let indexPath = tableName.indexPathForSelectedRow 
    let section = indexPath?.section 
    let row = indexPath?.row 
+0

這可能是最好的答案,因爲它使用已經存在的內容。更具體地將代碼更改爲OP代碼。 – ryantxr

相關問題