Search code examples
iosswiftpfquerytableviewcontrolle

Counting found objects in PFTableQueryViewController


I am trying to count the number of the found objects in PFQueryTableViewController.

I have tried working around with

override func queryForTable() -> PFQuery {
    let query = PFQuery(className: self.parseClassName!)
    query.whereKey("member", equalTo: memberId!)

    let count = query.countObjectsInBackground()
    label.text = "\(count)"


    return query

}

But my app will crash.

EDIT: The issue is not to make a query and count it's objects. The problem is to use queryForTable passing my query to cellForRowAtIndexPath of my PFQueryTableViewController

the cellForRowAtIndexPath looks like this:

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

    let cell:DetailApplicantCell = self.table.dequeueReusableCellWithIdentifier("reuseIdentifier") as! DetailApplicantCell

    if let name = object?.objectForKey(self.textKey!) as? String{
    cell.nameLbl.text = name
    }
    cell.groupImage.image = UIImage(named: "People.png")
    if let imageFile = object?.objectForKey(self.imageKey!) as? PFFile{
    cell.groupImage.file = imageFile
    cell.groupImage.loadInBackground()
    }

    return cell

}

NOTE that this is not the default cellForRow


Solution

  • Rather than doing a second PFQuery I found a better way using a method of PFQueryTableViewController like this:

        override func objectsDidLoad(error: NSError?) {
        super.objectsDidLoad(error)
    
        print("objectsDidLoad")
            if let results = self.objects{
            print("objectsFound")
    
            self.groupsCountLbl.text = "\(results.count)"
            self.groupsCountLbl.fadeIn()
        }
    }
    

    The VC has a property objects an array of AnyObject?. With the objectsDidLoad function you determine the time, everything is loaded.