如何在didSelectRowAtIndexPath之后运行prepareForSegue?

3
我正在尝试获取用户单击的indexPath.row,然后检查我的数组以从该indexPath.row返回一个字符串,然后通过prepareForSegue发送该字符串.... 但是它不起作用。 我认为程序在didselectRowatIndexPath之前运行了preparForSegue...我该怎么办? 我的代码:
import UIKit
import Parse


class CategoriaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()
var escolha = 5

override func viewDidLoad() {
    super.viewDidLoad()


    var query = PFQuery(className:"Categorias")
    //querycomlocalidade
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects!.count) Restaurantes.")
            if let _objects = objects as? [PFObject] {

                self.queryArray = _objects
                self.tableView.reloadData()

            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return queryArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CategoriaTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ceculaCategoria", forIndexPath: indexPath) as! CategoriaTableViewCell


    let categoria = queryArray[indexPath.row] as! PFObject

    //Insere texto
    cell.tituloCecula.text = categoria.objectForKey("nome") as! String


    //Insere imagens no tableview
    if let categoriaImagem = categoria.objectForKey("imagem") as? PFFile {

        categoriaImagem.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                cell.imagemCecula.image = UIImage(data:imageData!)                }
        }
    }


    return cell
}

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

    escolha = indexPath.row

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var detailScene = segue.destinationViewController as! ListaTableViewController

    if let indexPath = self.tableView.indexPathForSelectedRow() {
        let row = Int(indexPath.row)

        let categoriaEscolhida = queryArray[escolha] as PFObject

        //Insere texto

        detailScene.categoria = categoriaEscolhida.objectForKey("nome") as! String



    }

}



}

2
如果您忽略 escolha 并使用 queryArray[row] 会发生什么? - Phillip Mills
那么我怎么知道他选择了哪一行? - Renato Pimpão
1
你知道这是因为你正在调用 indexPathForSelectedRow - Phillip Mills
1
行包含行的索引。 - psobko
1
我怎么会错过那个...谢谢! - Renato Pimpão
显示剩余2条评论
3个回答

7
如果您直接从故事板中的单元格链接了segue,那么在触发segue之前,didSelectRowAtIndexPath将不会执行。
您可以通过在故事板中从视图控制器对象向目标场景进行Ctrl拖动并为segue设置标识符来创建segue,就像使用其他segue一样。从您的单元格中删除segue。
现在,您可以调用performSegueWithIdentifier来触发segue并将所选对象作为sender传递。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   // tableView.deselectRowAtIndexPath(indexPath)  // Optionally deselect the row for a cleaner appearance
    self.performSegueWithIdentifier("mySegueIdentifier", sender: queryArray[indexPath.row])
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let detailScene = segue.destinationViewController as? ListaTableViewController {          
        let categoriaEscolhida = sender as! PFObject
        detailScene.categoria = categoriaEscolhida.objectForKey("nome") as! String

    }    
}

1
你需要在两个视图控制器之间创建segue并手动触发它:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    escolha = indexPath.row
    self.performSegueWithIdentifier("push", sender: self)
}

0
 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    escolha = indexPath.row
    self.performSegueWithIdentifier("push", sender: self)
}

完整的程式碼如下...

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:"push"]) {
    // Code
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接