我有两个viewControllers。第一个是tableViewController,第二个是webViewController。我想使用segue将数据从第一台vc传递到第二台vc。由于这个原因,我希望从重写func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)的“chosenCellIndex”值准备prepareForSegue方法。但是我无法从prepareForSegue方法访问selectedCellIndex值。请帮忙。

类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)
}

}

最佳答案

为了获取选定的单元格索引,您需要获取所选行的索引路径,然后需要从indexpath获取节或行索引。

请参阅以下代码以获取行索引

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

关于ios - 如何从同一类中的prepareForSegue方法访问重写func tableView属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36776900/

10-12 04:40