[英]Deleting Table View Cell with Parse in Swift
I have my app, which I have users which will post questions. 我有我的应用,我的用户会发布问题。 I want the users to be able to delete their posts, which I sort of have been able to do.
我希望用户能够删除他们的帖子,而我确实做到了。 Here is my code.
这是我的代码。
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()
}
}
})
}
}
As expected my code deleted all the posts from the current user. 不出所料,我的代码删除了当前用户的所有帖子。 I don't know how to get just the selected post and delete only that one.
我不知道如何仅获取选定的帖子并仅删除该帖子。 Here is how the data base is structured
这是数据库的结构
{
"_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"
}
}
Thanks for your help! 谢谢你的帮助!
Code for Getting Data: 获取数据的代码:
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
}
You can get your object to delete in this way: 您可以通过以下方式删除对象:
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.