在 Swift 中使用解析删除 Table 查看单元格

Deleting Table View Cell with Parse in Swift

我有我的应用程序,我的用户会 post 提出问题。我希望用户能够删除他们的 post,而我已经做到了。这是我的代码。

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
    if editingStyle == UITableViewCellEditingStyle.delete{
        print("here1")
        let query = PFQuery(className: "Post")
        query.whereKey("user", equalTo: PFUser.current())
        print("here2")
        query.findObjectsInBackground(block: { (objects, error) in
            if error != nil {
                print("THERE WAS AN ERROR DELETING THE OBJECT")
            }else{
                for object in objects!{
                    print("here3")
                    object.deleteInBackground()
                    tableView.reloadData()
                }
            }
        })
    }
}

正如预期的那样,我的代码删除了当前用户的所有 post。我不知道如何只获取选定的 post 并只删除那个。 这是数据库的结构

{
"_id": "EZY40tN2FR",
"location": [
    -122.0312186,
    37.33233141
],
"question": "Sldfksdlkfjsldfkjsldkfjsldkjf",
"category": "Entertainment",
"_p_user": "_User$R9GdCYnVno",
"_created_at": {
    "$date": "2017-10-17T01:03:11.438Z"
},
"_updated_at": {
    "$date": "2017-10-17T01:03:11.438Z"
}
}

感谢您的帮助!

获取数据的代码:

override func queryForTable() -> PFQuery<PFObject> {
        let query = PFQuery(className: "Post")
        query.includeKey("user")
        query.whereKey("user", equalTo: PFUser.current()!)
        query.order(byDescending: "createdAt")
        query.limit = 100
        return query
    }

您可以通过以下方式删除您的对象:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
    if editingStyle == UITableViewCellEditingStyle.delete{
        print("here1")
        let objectToDelete = objects?[indexPath.row] as! PFObject
        objectToDelete.deleteInBackgroundWithBlock { (success, error) in
            if (success) {
                // Force a reload of the table - fetching fresh data from Parse platform
                self.loadObjects()
            } else {
                // There was a problem, check error.description
            }
        }

    }
}