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

Ios Xcode解析查询按钮从单元格中删除文本和照片,ios,swift,xcode,parse-platform,swift4,Ios,Swift,Xcode,Parse Platform,Swift4,每个单元格显示登录用户的相应评论和照片。它们加载了parse 现在,您需要Löschen按钮删除照片和评论 不幸的是,这不起作用。我点击按钮什么也没发生 不幸的是,我对斯威夫特了解甚少,无法找到解决方案 查询工作正常,应用程序显示照片和共享内容。查询和邮政编码: override func viewDidLoad() { super.viewDidLoad() super.viewDidLoad() let query = PFQuery(className: "Post") query.w

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

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

不幸的是,这不起作用。我点击按钮什么也没发生

不幸的是,我对斯威夫特了解甚少,无法找到解决方案

查询工作正常,应用程序显示照片和共享内容。查询和邮政编码:

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

        }
    }
})
}

下面是我尝试过的“删除”功能代码:

   @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。 因此,基于此,您可以轻松删除

实现delete函数更简单的方法是为您的消息设置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()
  }
}
用不同的数组表示同一个对象并不是一件好事。所以,更好的解决问题的方法是只为您的帖子设置一个数组

取回时,您可以执行以下操作:

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
    }
})
当您想在“删除”功能中删除它时,使用这种方式:

if indexPath.row < self.posts.count {
    self.posts[indexPath.row].deleteInBackground()
}
如果indexath.row
您无权访问当前的索引位置和对象ID。 因此,基于此,您可以轻松删除

实现delete函数更简单的方法是为您的消息设置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()
  }
}
用不同的数组表示同一个对象并不是一件好事。所以,更好的解决问题的方法是只为您的帖子设置一个数组

取回时,您可以执行以下操作:

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
    }
})
当您想在“删除”功能中删除它时,使用这种方式:

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