gpt4 book ai didi

ios - 从 TableView 行获取 ObjectId(解析)

转载 作者:行者123 更新时间:2023-11-30 14:08:53 26 4
gpt4 key购买 nike

我有一个名为“Restaurantes”(英语餐厅)的解析类,目前我可以在 CustomTableView 中列出所有餐厅。

现在我的目标是当我从列表中选择一家餐厅时,它会显示每个餐厅的“菜单”。为此,我必须使用prepareForSegue传递餐厅ObjectId,并在新的ViewController中进行查询以列出该餐厅ObjectId的所有“菜单”。

如何获取用户点击的餐厅的ObjectId? ( swift )

我的代码:

import UIKit
import Parse
import AVFoundation

class ListaTableViewController: UITableViewController {

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


override func viewDidLoad() {
super.viewDidLoad()
var query = PFQuery(className:"Restaurantes")
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) -> ListaTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell

//Insert text in tableview
let restaurante = queryArray[indexPath.row] as! PFObject
cell.textCell.text = restaurante.objectForKey("nome") as! String



//Insert images in tableview
if let userPicture = restaurante.objectForKey("imagem1") as? PFFile {

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


return cell
}

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

if segue.identifier == "cell" {
if let destination = segue.destinationViewController as? MenuPageViewController {
if let blogIndex = tableView.indexPathForSelectedRow()?.row {




}
}
}

}


}

最佳答案

应该是这样的

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

if segue.identifier == "cell" {
if let destination = segue.destinationViewController as? MenuPageViewController {
if let blogIndex = tableView.indexPathForSelectedRow()?.row {
// Get the object restaurant object from your array
let restaurante = queryArray[blogIndex] as! PFObject
// set object id to the property in destination
destination.resturantId = restaurant.objectId
}
}
}

}

关于ios - 从 TableView 行获取 ObjectId(解析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com