使用指针查询解析对象,swift

时间:2016-02-23 18:02:44

标签: swift parse-platform tableview

我正在尝试使用Parse中的指针进行查询。我有两个类“Discover”和“DiscoveryDetails”。我想获取从发现类中挑选的对象的发现细节。

发现课程 enter image description here

DiscoveryDetails类 - 将discoverID作为指针。 enter image description here

发现对象显示在DiscoveryTableView中,在选择其中一个项目时,我想在DiscoveryDetailsTableView中查询具有与该选择相关的ID的对象。

DiscoveryTableView显示类中出现的对象,但DiscoveryDetailsTableView显示所有对象,而不是与我选择的Cell相关的对象。

这是DiscoveryTableView中的didSelectRowAtIndexPath代码:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

                let mainStoryboard = UIStoryboard(name: "Discovery", bundle: nil)
                let discoveryDetailView = mainStoryboard.instantiateViewControllerWithIdentifier("discoveryDetailTVC") as! DiscoveryDetailTableViewController

                let object = self.objectAtIndexPath(indexPath)
                discoveryDetailView.titleString = object?.objectForKey("workoutName") as! String
                discoveryDetailView.describtionString = object?.objectForKey("workoutDetails") as! String
                discoveryDetailView.numberOfWorkouts = object?.objectForKey("numberOfWorkouts") as! Int
                discoveryDetailView.imageFile1 = object?.objectForKey("image1") as! PFFile
                discoveryDetailView.imageFile2 = object?.objectForKey("image2") as! PFFile
                discoveryDetailView.imageFile3 = object?.objectForKey("image3") as! PFFile

let row = indexPath.row //we know that sender is an NSIndexPath here.
            let selectedObj = objects![row] // some var where you hold your data
            discoveryDetailView.varInDDT = selectedObj


                self.navigationController?.pushViewController(discoveryDetailView, animated: true)

        }

在我的DiscoveryDetailsTableView中,我有以下代码:

var titleString: String!
    var describtionString: String!
    var numberOfWorkouts: Int!
    var imageFile1: PFFile!
    var imageFile2: PFFile!
    var imageFile3: PFFile!
   var varInDDT : PFObject?

    //MARK: Query for Table with the details
override func queryForTable() -> PFQuery {

    let discoveryQuery = PFQuery(className: "DiscoveryDetails")
    discoveryQuery.cachePolicy = .CacheElseNetwork
    discoveryQuery.whereKey("discoveryID", equalTo: PFObject(withoutDataWithClassName: "Discovery", objectId: "\(varInDDT!.objectId!)"))
    discoveryQuery.orderByDescending("createdAt")
    return discoveryQuery

}

override func viewDidLoad() {
        super.viewDidLoad()

        //Header Display
        let imagesArray = [imageFile1, imageFile2, imageFile3]
        let imagePicked = randomIntergerInRange(0, high: imagesArray.count)

        titleLabel.text = titleString.uppercaseString
        self.subtitleLabel.text = describtionString
        self.numberOfWorkoutsLabel.text = "\(numberOfWorkouts!) Workouts"

        //Pick a random Image from the Images
        imagesArray[imagePicked].getDataInBackgroundWithBlock({ (imageData, error) -> Void in
            if error == nil
            {
                if let imageData = imageData
                {
                    let image = UIImage(data:imageData)
                    self.backgroundImage.image = image
                }
            }
        })

...

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

var discoveryDetailItemsCell:DiscoveryDetailTableViewCell! = tableView.dequeueReusableCellWithIdentifier("DiscoveryDetailTableViewCell") as? DiscoveryDetailTableViewCell

...

discoveryDetailItemsCell.titleLabel.text = object?.objectForKey("exerciseName") as? String
        discoveryDetailItemsCell.titleLabel.textColor = UIColor.whiteColor()

        discoveryDetailItemsCell.durationAndSetsLabel.text = "\((object?.objectForKey("durationOrSets"))!)"
        discoveryDetailItemsCell.minAndSetLabel.text = "mins"


...

return discoveryDetailItemsCell

}

可能存在类似的问题,但我没有发现任何可以解决这个问题的问题,或者我可能没有清楚地看到我的错误。

提前感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:1)

对于带有指针U的查询,

必须使用类似

的查询
let discoveryQuery = PFQuery(className: "DiscoveryDetails")
discoveryQuery.cachePolicy = .CacheElseNetwork 
discoveryQuery.whereKey("discoveryID", equalTo: PFObject(withoutDataWithClassName: "Discovery", objectId: "\(varInDDT!.objectId!)"))
discoveryQuery.orderByDescending("createdAt")
return discoveryQuery

用于下载详细信息,您必须至少将要从第一个viewController下载的对象的ID传递给第二个或者您可以传递整个PFObject

<强> DiscoveryTableView

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if(segue.identifier == "showDetail"){ // define the segue Name
    let controller = (segue.destinationViewController as! DiscoveryDetailTableViewController)
    let row = sender!.row //we know that sender is an NSIndexPath here.
    let selectedObj = discoveryObjects[row] // some var where you hold your data
    controller.varInDDT = selectedObj
  }
}

并在detailVC

中定义PFObject的var

<强> DiscoveryDetailTableViewController

var varInDDT : PFObject?