我正在尝试计算在 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/