簡體   English   中英

在Swift中使用解析刪除表視圖單元格

[英]Deleting Table View Cell with Parse in Swift

我有我的應用,我的用戶會發布問題。 我希望用戶能夠刪除他們的帖子,而我確實做到了。 這是我的代碼。

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

不出所料,我的代碼刪除了當前用戶的所有帖子。 我不知道如何僅獲取選定的帖子並僅刪除該帖子。 這是數據庫的結構

{
"_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
            }
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM