Xcode 解析查询按钮从单元格中删除文本和照片

Xcode Parse Query button Delete text and photo from a cell

每个单元格显示登录用户的相应评论和照片。他们加载了解析。

现在您想要 Löschen 按钮删除照片和评论。

不幸的是,这不起作用。我点击按钮没有任何反应

不幸的是,我对 swift 知之甚少,无法找到答案

查询有效,应用程序显示照片和 Commons.The 查询和 post 代码:

override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
let query = PFQuery(className: "Post")

query.whereKey("username", equalTo: PFUser.current()?.username)
query.findObjectsInBackground(block: { (object, error) in

    if let posts = object {
        for post in posts{
            print(posts)

            self.comments.append(post["message"] as! String)
            self.imageFile.append(post["imageFile"] as! PFFile)
            self.tableView.reloadData()
        }


        DispatchQueue.main.async {
            self.tableView.reloadData()

        }
    }
})

}

这里是我试过的 "delete" 函数代码:

   @IBAction func remove(_ sender: Any) {
    print("Entered remove")
    let query = PFQuery(className: "Post")
    query.whereKey("username", equalTo: PFUser.current()?.username)
    query.findObjectsInBackground(block: { (object, error) in

        if let posts = object {
            print(posts)
            for post in posts{
                print(posts)

                if let message = post["message"] as? String, let image = post["imageFile"] as? PFFile {
                    print("message and image read", message, image)
                    if let messageIndex = self.comments.index(of: message), let imageIndex = self.imageFile.index(of:image) {
                        self.comments.remove(at: messageIndex)
                        self.imageFile.remove(at: imageIndex)

                    }
                }

                self.tableView.reloadData()

            }

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    })
}

输出:

我没有收到错误消息,也没有删除任何内容。 感谢您的帮助

您无权访问当前的索引位置和对象 ID。 因此,基于此,您可以轻松删除。

实现删除功能更简单的方法是为您的消息设置一个 objectId 数组:

self.ids.append(post.objectId)

当你想删除它时:

let query = PFQuery(className: "Post")
query.whereKey("objectId", equalTo: self.ids.index(of: indexPath.row))
// Make a query in background to only get the object that you want to delete 
query.getFirstObjectInBackgroundWithBlock {
  (object: PFObject?, error: NSError?) -> Void in
  if error != nil || object == nil {
    print("The getFirstObject request failed.")
  } else if let object = object {
    print("Successfully retrieved the object.")
    object.deleteInBackground()
  }
}

Having different arrays representing the same object is not really good to do. So a better way to handle you problem is have only one array for your post

当你获取它时,你可以做类似的事情:

guard let user = PFUser.current() else { return }
let query = PFQuery(className: "Post")
query.whereKey("username", equalTo: user.username)
query.findObjectsInBackground(block: { (posts, error) in
    if let posts = posts {
        self.posts = posts
    }
})

在remove函数中删除时使用这种方式:

if indexPath.row < self.posts.count {
    self.posts[indexPath.row].deleteInBackground()
}