0

我正在尝试计算在PFQueryTableViewController.

我试过解决

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

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


    return query

}

但是我的应用程序会崩溃。

编辑: 问题不是进行查询并计算它的对象。问题是使用queryForTable将我的查询传递给cellForRowAtIndexPath我的PFQueryTableViewController

cellForRowAtIndexPath看起来像这样:

   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

}

请注意,这不是默认的 cellForRow

4

3 回答 3

1

尝试使用query.findObjectsInBackgroundWithBlock方法并获取size()响应对象的

        let query = PFQuery(className: self.parseClassName!)
        query.whereKey("member", equalTo: memberId!)
        query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    let count = objects.size()
                    label.text = "\(count)"
                    if let object = objects as? [PFObject] {

                    }
                } else {
                    // Log details of the failure
                    print("Error: \(error!)")
                }
         }
于 2016-08-25T12:58:02.447 回答
0

您在 2 个地方强制展开,使用if let

func queryForTable() -> PFQuery? {
   if let parseClass = self.parseClassName {
      let query = PFQuery(className: parseClass)
      if let id = memberId {
         query.whereKey("member", equalTo: id)
      }

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

然后你使用你的功能,如:

if let query = queryForTable() {
    //your query related code here.
}
于 2016-08-25T13:03:58.293 回答
0

而不是做一秒钟PFQuery,我找到了一种更好的方法,使用这样的方法PFQueryTableViewController

    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()
    }
}

VC 有一个属性objects数组AnyObject?。使用objectsDidLoad您确定时间的功能,一切都已加载。

于 2016-08-25T14:31:39.430 回答