【问题标题】:How to access override func tableView property from prepareForSegue method inside same class如何从同一个类中的 prepareForSegue 方法访问覆盖 func tableView 属性
【发布时间】:2016-08-15 02:03:18
【问题描述】:

我有两个 viewController。第一个是 tableViewController,第二个是 webViewController。我想使用 segue 将数据从第一个 vc 传递到第二个 vc。出于这个原因,我希望从 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 到 prepareForSegue 方法的“chosenCellIndex”值。但我无法从 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)
}

}

【问题讨论】:

  • 我只需要将“chosenCellIndex”变量的值从 func tableView 获取到 func prepareForSegue。但是为什么 func tableView 和 func prepareForSegue 中“chosenCellIndex”的值不一样。这是我的问题。

标签: ios iphone swift uitableview segue


【解决方案1】:

确保您已在情节提要中的 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()。

【讨论】:

    【解决方案2】:

    最好的方法是从确实选择行函数,那个例子

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

    【讨论】:

    • 我只需要从 func tableView 获取 "chosenCellIndex" 变量的值到 func prepareForSegue。但是为什么 func tableView 和 func prepareForSegue 中“chosenCellIndex”的值不一样。这是我的问题。
    【解决方案3】:

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

    查看以下获取行索引的代码

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

    【讨论】:

    • 这可能是最好的答案,因为它使用了已经存在的东西。使代码更具体地适应 OP 代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多