使用 Swift 3 在 Parse 中删除一行

Delete a single row in Parse using Swift 3

我需要一些帮助!我似乎无法从 Parse 中删除一行。我弄清楚了 "Swipe to delete",但是,当您尝试从 table 中删除某些内容时,它什么也做不了。我没有错误。什么都不会被删除。这是我的代码。

      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // delete from server
        let myFeatherquery = PFQuery(className: "FeatherPosts")

        myFeatherquery.whereKey("message", equalTo: (PFUser.current()?.objectId!)!)
        myFeatherquery.findObjectsInBackground(block: { (objects, error) in
            if error != nil {
                print("THERE WAS AN ERROR")
            }else{
                for object in objects!{
                    self.messages.remove(at: indexPath.row)
                    object.deleteInBackground()
                    self.tableView.reloadData()
                }
            }
        })
    }
}

简而言之,我想从 tableView 中删除一个 post 并在解析端也删除它。如果我改变:

"myFeatherquery.whereKey("message", equalTo: (PFUser.current()?.objectId!)!)"

"myFeatherquery.whereKey("userid", equalTo: (PFUser.current()?.objectId!)!)"

它只是删除了用户曾经 post编辑的所有内容。请帮忙!

你不需要在UITableViewCellEditingStyle里面查询,因为IndexPath已经确定你要删除哪一个。

现在我为这个逻辑添加了一些额外的内容。

1:) 可以滑动单元格查看删除按钮。单击后将确认您是否要删除。
2:) 一旦删除,就会发生淡入淡出。然后它将刷新 tableView 并在后台删除解析中的对象。

FeatherPostsArray你看我做的是你在tableView中用到的对象数组。在您的 numberOfRowsInSection 中,您会对其进行计数。

所以它应该是这样的:

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        var recClass = PFObject(className:"FeatherPosts")
        recClass = self.FeatherPostsArray[(indexPath as NSIndexPath).row]



        let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
            let alert = UIAlertController(title: "App Name",
                                          message: "You sure you want to delete?",
                                          preferredStyle: .alert)

            let delete = UIAlertAction(title: "Delete", style: .default, handler: { (action) -> Void in

                recClass.deleteInBackground {(success, error) -> Void in
                    if error != nil {

                    }}

                self.FeatherPostsArray.remove(at: (indexPath as NSIndexPath).row)
                tableView.deleteRows(at: [indexPath], with: .fade)
                tableView.reloadData()
            })
            let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })
            alert.addAction(delete)
            alert.addAction(cancel)
            self.present(alert, animated: true, completion: nil)

        }

        //This is nice if you want to add a edit button later
        return [ deleteAction]

    }

如果您遇到困难,请告诉我。