2017-02-03 21 views
0

我需要一些帮助!我似乎无法从Parse中删除一行。然而,当我尝试删除表格中的某些内容时,我没有想到“滑动删除”,但它没有执行任何操作。我没有得到任何错误。没有被删除。这是我的代码。使用Swift在Parse中删除单行3

 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中删除一篇文章,并在解析端删除它。如果我改变:

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

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

它只是删除了用户曾经发布了一切。请帮忙!

回答

0

您不需要在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] 

    } 

让我知道你是否会被卡住。

+0

这工作。我不得不稍微调整一下,但效果很好!谢谢! –

+0

欢迎队友! – Cliffordwh