从PFTableViewController中删除图像时出现此错误。我已经做了大量的搜索,我可以告诉commitEditingStyle中的代码是正确的。有人可以帮忙吗?
import Foundation
class profileImageTableViewController:PFQueryTableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@IBOutlet weak var MenuViewButton: UIBarButtonItem!
@IBOutlet weak var addImageButton: UIBarButtonItem!
let imagePicker = UIImagePickerController()
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "ProfileImages"
self.textKey = "pImage"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "ProfileImages")
var userid = PFUser.currentUser()?.objectId
query.whereKey("userid", equalTo: userid!)
query.whereKey("imageType", notEqualTo: "profileThumb")
query.orderByAscending("createdAt")
return query
}
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
//println( myStruct )
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "imageCropSegue"{
if let destination = segue.destinationViewController as? profileImageCropView {
if let profileIndex = tableView.indexPathForSelectedRow()?.row {
var index = NSIndexPath(forRow: profileIndex, inSection: 0)
var cell = tableView.cellForRowAtIndexPath(index) as! profileImageCell
println(destination.profileImageView)
destination.profileImageView.image = cell.profileImageView.image //currently generates an error (separate issue)
}
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("profileImages") as! profileImageCell!
if cell == nil {
cell = profileImageCell(style: UITableViewCellStyle.Default, reuseIdentifier: "profileImages")
}
var iType = object["imageType"] as! String
//println(iType)
if iType == "profile"{
cell.profileImageButton.hidden = true
}
var file:PFFile = object["pImage"] as! PFFile
cell.profileImageView.image = UIImage(named: file.name)
cell.profileImageView.file = file
cell.profileImageView.loadInBackground(nil)
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
let objectToDelete = objects?[indexPath.row] as! PFObject
objectToDelete.deleteInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
self.loadObjects()
} else {
println(error)
}
}
}
}
@IBAction func addImageTapped(sender: UIBarButtonItem) {
if(self.objects?.count < 5){
self.imagePicker.allowsEditing = false
self.imagePicker.sourceType = .PhotoLibrary
self.imagePicker.modalPresentationStyle = .Popover
presentViewController(imagePicker, animated: true, completion: nil)
self.imagePicker.popoverPresentationController?.barButtonItem = sender
}else{
var alert = UIAlertController()
self.presentViewController(alert.isNotification("Max images reached", buttonText: "ok", preferredStyle: UIAlertControllerStyle.Alert), animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
var data:NSData = UIImageJPEGRepresentation(pickedImage, 0.5)
var imageFile = PFFile(name: "profileimage.jpg", data: data)
//imageView.contentMode = .ScaleAspectFill
//imageView.image = pickedImage
var myObject = PFObject(className: "ProfileImages")
myObject["authorised"] = false
myObject["userid"] = PFUser.currentUser()?.objectId!
myObject["imageType"] = "gallery"
myObject["pImage"] = imageFile
myObject.saveInBackgroundWithBlock{(success:Bool,error:NSError?) -> Void in
self.loadObjects()
}
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
当我运行模拟器时,图像会从解析中删除,但一旦发生这种情况,我就会收到错误。
https://drive.google.com/file/d/0Bxds5-29-lhIR0I3MGswdFE1LUU/view?usp=sharing
这是loadObjects()
的断点答案 0 :(得分:1)
我有完全相同的问题。
最后,我意识到我忘了删除&#39; objectToDelete&#39;来自&#39;对象&#39;在重新加载tableView之前。
答案 1 :(得分:0)
我无法找到答案。最后,我最终使用了标准的UITableView,它可以毫无错误地删除行。
我原本认为PFQueryTableViewController的创建是为了更容易使用Parse的Table Views,但令人惊讶的是我能够用UITableView做同样的事情而几乎没有额外的代码。