从 TableView 行获取 ObjectId (Parse)

Posted

技术标签:

【中文标题】从 TableView 行获取 ObjectId (Parse)【英文标题】:Get ObjectId (Parse) from TableView row 【发布时间】:2015-08-13 14:35:58 【问题描述】:

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

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

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

我的代码:

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 




            
        
    





【问题讨论】:

不要传递objectId,传递整个对象。并且该对象应该与您可以显式查询的菜单有关系... 【参考方案1】:

应该是这样的

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
                
            
    


【讨论】:

想象在viewDidLoad 和用户点击一行之间,有人添加了一家新餐厅。 prepareForSegue 查询将返回错误的餐厅。 =/ 最佳实践表明,如果您正在更改数据源,那么您还应该将这些更改应用于 tableView,反之亦然......所以如果您在数组中添加行,那么您还应该将这些行插入到表... 这不是所有应用程序的最佳做法。在我的例子中,用户可以添加餐厅,其他用户可以列出可用的餐厅并可以下拉刷新。 当您填充 tableView 并在您的 case queryArray 中设置 dataSource 时,您的 dataSource 和 tableView 应该匹配,否则您将创建一些异常。因为用户将看到选项一并选择该选项,但您的底层数据结构将不一样,这不是正确的行为。

以上是关于从 TableView 行获取 ObjectId (Parse)的主要内容,如果未能解决你的问题,请参考以下文章

使用核心数据和数组从 TableView 中插入和删除行

从 Tableview 单元格中获取超过一个 TextField 的值?

使用 TableView 从 ControlView 获取字符串

无法删除 tableview 行

根据登录用户的 ObjectID 从 Mongo 获取数据

从javascript中的键和字符串值获取objectId [重复]