gpt4 book ai didi

ios - 使用 canEditRowAtIndexPath 从解析中删除图像

转载 作者:行者123 更新时间:2023-11-30 14:16:01 24 4
gpt4 key购买 nike

我目前正在使用 swift(parse) 编写一个类似于 instagram 的小项目。用户发布图像后,将其上传以进行解析,然后我将其检索到表格 View 中。由于图像需要转换为 NSData 和 PFFile,因此它被保存为 PFFile。我试图通过 objectToDelete.deleteInBackgroundWithBlock 使用 canEditRowAtIndexPath 从 TableView 解析中删除文件。然而,根据我的研究,我发现 PFFile 无法从用户中删除,只能删除对象。但是图像文件对于对象来说太大了。 (我希望我是对的)。我想知道如何让它在用户不想看到它或删除帖子后不显示在表格 View 中。

更新

 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
println("Commit Editing Style \(editingStyle)")
}

func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]! {



var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {
(action: UITableViewRowAction!, indexPath: NSIndexPath!) in
println("Triggered delete action \(action) atIndexPath: \(indexPath)")
return
})

return [deleteAction]
}


func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {


var fileName = "uploaded_image.png"
let applicationID = "appidwritten"
let masterKey = "masterkeywritten"

let request = NSMutableURLRequest(URL: NSURL(string: "https://api.parse.com/1/files/\(fileName)")!)
request.HTTPMethod = "DELETE"
request.setValue(applicationID, forHTTPHeaderField: "X-Parse-Application-Id")
request.setValue(masterKey, forHTTPHeaderField: "X-Parse-Master-Key")
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
println(response)
}).resume()

}


}

谢谢

最佳答案

您无法通过deleteInBackgroundWithBlock删除文件:

Why can't I delete a file using the SDK?

Since files do not have ACLs, their write access cannot be restricted to a specific user. Since leaving files open to deletion by any arbitrary user is not a great security measure, we require that a master key is used to delete a file. Since client SDKs cannot use the master key, you will need to use the REST API.

https://parse.com/questions/how-can-i-delete-a-file

因此您应该通过 REST API 删除文件,如下所示:

var fileName = ""
let applicationID = ""
let masterKey = ""

let request = NSMutableURLRequest(URL: NSURL(string: "https://api.parse.com/1/files/\(fileName)")!)
request.HTTPMethod = "DELETE"
request.setValue(applicationID, forHTTPHeaderField: "X-Parse-Application-Id")
request.setValue(masterKey, forHTTPHeaderField: "X-Parse-Master-Key")
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
println(response)
}).resume()

文件名必须是上传操作响应中的名称,而不是原始文件名。

关于ios - 使用 canEditRowAtIndexPath 从解析中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com