2016-04-21 57 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

相关问题