I have a Parse Class named "Restaurantes" (Restaurants in english) and currently i can list all the restaurants in a CustomTableView.
Now my goal is when i choose a restaurant from the list, it shows the "menus" of each restaurant. For this I have to pass the Restaurant ObjectId using prepareForSegue and in the new ViewController have a query to list all the "menus" for that restaurant ObjectId.
How can I get the ObjectId of the restaurant that user clicked? (swift)
My Code:
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 {
}
}
}
}
}
it should be something like this
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
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.