简体   繁体   中英

Deleting an object in a tableView cell from Parse

I am attempting to delete certain objects from Parse. The user will be able to save posts and I am trying to implement a way for them to delete them. The saved posts can be found in a TableView.

I currently have the following.

 override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let deleteAction = UITableViewRowAction(style: .Default, title: "Remove") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

        let query = PFQuery(className: "SavedObjects")
        query.orderByDescending("createdAt")
        query.whereKey("savedByUser", equalTo: (PFUser.currentUser()?.username)!)
        query.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error: NSError?) -> Void in
            if error == nil {

                for object in objects! {

                    object.deleteInBackground()
                }
            }
        })

        self.tableView.reloadData()
    }

    deleteAction.backgroundColor = UIColor.redColor()


    return [deleteAction]

}

The object.deleteInBackground() does work, but it deletes all objects that were saved by the user. I would like it to only delete the object that the cell represents.

I have also tried to use object.deleteInBackgroundWithTarget([indexPath.row], selector: nil) but have had no luck.

if I understood your question correctly, I think you need to add another query.WhereKey() filtering by the objectId of the corresponding row saved on Parse. This way the call to object.deleteInBackground() will only delete the cell you selected from the TableView. I hope this was helpful!

Valerio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM