我正在尝试获取用户单击的
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
}
}
}
escolha
并使用queryArray[row]
会发生什么? - Phillip MillsindexPathForSelectedRow
。 - Phillip Mills