如何从UITableView单元中检索解析对象并将其呈现在详细信息视图控制器中

时间:2015-07-28 01:45:36

标签: ios xcode swift parse-platform

我有一个UITableView,其中包含PFObjects列表(它们是组名),当用户点击其中一个单元格时,我希望所有来自该特定所选对象的对象信息被检索并呈现在详细视图控制器中。我将不胜感激任何帮助!

class GroupNamesTable: UITableViewController{

    let cellidentifier = "Cell"

    var dataparse: NSMutableArray = NSMutableArray()

    func loaddata () {    
        var findgroups: PFQuery = PFQuery(className: "BeaterGroups")
        findgroups.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in    
            if error == nil && objects != nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        self.dataparse.addObject(object)
                    }
                }
            }
            self.tableView.reloadData()
        }        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        loaddata()
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as! UITableViewCell
        let cellDataParse: PFObject = self.dataparse.objectAtIndex(indexPath.row) as! PFObject
        cell.textLabel?.text = cellDataParse.objectForKey("GroupName")! as? String
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("GroupInfoSegue", sender: self)
        let indexPath = tableView.indexPathForSelectedRow()
        let currentcell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
        println(currentcell.textLabel!.text!)
        var query = PFQuery(className: "BeaterGroups")
        query.whereKey("GroupName", equalTo: currentcell.textLabel!.text!)
        query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                println(objects)
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)
                    }
                }
            } else {
                println("Error: \(error!)")
            }
    } 
}

1 个答案:

答案 0 :(得分:0)

您应该只调用tableView performSegueWithIdentifier()中的didSelectRowAtIndexPath并使用BeaterGroups方法获取prepareForSegue()对象:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("GroupInfoSegue", sender: self)
}

获取对象时,您不需要再次查询它,因为您已经在dataparse数组中拥有它。只需使用所选单元格的indexPath并将对象从数组中取出:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "GroupInfoSegue") {
        // get indexPath of selected cell
        let indexPath = tableView.indexPathForSelectedRow()

        // get object from your dataparse array using the indexPath
        var groupObject = self.dataparse[indexPath!.row]

        println(groupObject.objectId)

        // pass the object to your destination view controller...
        ....
    }
}