I have a table view that displays objects from Parse. I have a button that modally presents another view controller for adding new objects. When a row in the table view is selected, it modally presents a view controller to display the objects info and allows the object to be edited. The issue I'm having is that when I add a new item or edit an item and then dismiss the modal controller, the table view reloads and displays the data multiple times. What have I done wrong?
These functions are in the presenting view controller (my table view controller). Obviously the issue has to be in didDismissViewController().
func getItems() {
// activityIndicator?.startAnimating()
let query = PFQuery(className: "Items")
query.whereKey("user", equalTo: PFUser.currentUser()!)
query.whereKey("collection", equalTo: self.title!)
query.orderByAscending("item")
query.limit = limit
query.skip = skip
query.findObjectsInBackgroundWithBlock( {
(objects, error) -> Void in
if error == nil {
if let objects = objects as [PFObject]! {
for object in objects {
self.itemNames.append(object["item"] as! String)
self.objectID.append(object.objectId!)
self.itemUPC.append(object["upc"] as! String)
self.model.append(object["model"] as! String)
self.pricePaid.append(object["pricePaid"] as! String)
// self.imagesArray.append(object["images"])
if object["images"] == nil {
self.imagesArray.append(nil)
} else {
self.imagesArray.append(object["images"])
}
}
}
// print(self.imagesArray)
/*
if objects!.count == self.limit {
self.skip += self.limit
self.getCollections()
}
*/
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
// self.activityIndicator!.stopAnimating()
print(self.itemNames)
}
} else {
var errorString = String()
if let message = error!.userInfo["error"] {
errorString = message as! String
}
print(errorString)
}
})
}
func createItem() {
let createVC = CreateItemViewController(nibName: "CreateItemViewController", bundle: nil)
let navController = NavController(rootViewController: createVC)
navController.modalPresentationStyle = .OverCurrentContext
createVC.collectionString = self.title!
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didDismissViewController", name: "ItemAdded", object: nil)
self.presentViewController(navController, animated: true, completion: nil)
}
func didDismissViewController() {
objectID.removeAll()
itemNames.removeAll()
itemUPC.removeAll()
pricePaid.removeAll()
model.removeAll()
imagesArray.removeAll()
itemImages.removeAll()
getItems()
}
This function is in the modally presented view controller where you enter item details for the Parse object you want to create.
func done() {
// Save the item data...
if itemImageView.image != nil {
let pictureData = UIImageJPEGRepresentation(itemImageView.image!, 0.05)
image = PFFile(name: "img", data: pictureData!)
} else {
image = nil
}
let newItem = PFObject(className: "Items")
newItem["item"] = itemNameTextField.text
newItem["upc"] = upcTextField.text
newItem["model"] = modelTextField.text
newItem["pricePaid"] = pricePaidTextField.text
newItem["user"] = PFUser.currentUser()
newItem["collection"] = collectionString
newItem["images"] = itemImages
newItem.saveInBackgroundWithBlock( {
(succeeded, error) -> Void in
if error == nil {
NSNotificationCenter.defaultCenter().postNotificationName("ItemAdded", object: nil, userInfo: nil)
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
} else {
var alert: UIAlertController?
let errorOK = UIAlertAction(title: "OK", style: .Default, handler: {
(action) -> Void in
alert?.dismissViewControllerAnimated(true, completion: nil)
})
var errorString = String()
if let message = error!.userInfo["error"] {
errorString = message as! String
}
alert = UIAlertController(title: "Error", message: errorString, preferredStyle: .Alert)
alert!.addAction(errorOK)
self.presentViewController(alert!, animated: true, completion: nil)
}
})
}
看来您正在添加观察者,但没有删除他们。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.