ios - 如何从同一个类中的 prepareForSegue 方法访问覆盖 func tableView 属性

标签 ios iphone swift uitableview segue

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

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

最佳答案

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

获取行索引见下面的代码

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

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

相关文章:

ios - 将数据从一个 Collection View 传递到嵌入式 Collection View

ios - 应用程序可能会从后台返回什么样的 "zombie"?

swift - 多上下文麻烦。无法让两个队列(主队列和私有(private)队列)在 Core Data 中保存数据

ios - 我如何在真正的预提交处理程序中修复 “[App] if we' re,由于 CA 限制,我们实际上无法添加任何新的围栏”?

ios - UITabBar.appearance().selectionIndicatorImage 设置的 UITabBarItem 背景在 iPhone X 上未对齐

iphone - 在 iPhone 上进行 GPU 加速计算 (GPGPU) 的资源?

ios - pageViewController viewControllerAfter/Before dataSource 方法被调用两次

iphone - 关于不使用 Interface Builder 进行 iPhone GUI 设计的教程?

iphone - 如何在选项卡 View Controller Storyboard应用程序中添加更多选项卡?

ios - 与容器 View 连接的点击手势识别器不会阻止容器 View 中按钮的触摸事件,但会阻止工具栏按钮的触摸事件