ios - 计算在 PFTableQueryViewController 中找到的对象

标签 ios swift pfquerytableviewcontrolle

我正在尝试计算在 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 将我的查询传递给 PFQueryTableViewController

cellForRowAtIndexPath

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

最佳答案

尝试使用 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!)")
                }
         }

关于ios - 计算在 PFTableQueryViewController 中找到的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39145862/

相关文章:

ios - 在 View Controller 之外选择 UISegmentedController 段

json - 用 Codable 解码这种 JSON

iOS 自动布局 View 高度基于较大的 subview 高度

swift - 如何仅在另一个函数完成时才调用一个函数? (Swift 并从 Parse 中检索)

swift - Parse 中的数据未加载到 PFQueryTableViewController 中

ios - 播放 m3u8 播放列表时 AVPlayer 会自动调整吗?

ios - 检查字符的 Unicode 值范围

ios - 解析/swift : PFQueryTableViewController createdAt object not working

ios - UITableView 仅反弹底部边缘

ios - 在 BLE 中读取和写入数据以快速打开/关闭 LED