我有一个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!)")
}
}
}
答案 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...
....
}
}