我有一个表视图,显示来自Parse的对象。我有一个按钮,以模态方式呈现另一个视图控制器来添加新对象。当选择表视图中的行时,它以模态方式呈现视图控制器以显示对象信息并允许对象进行编辑。我遇到的问题是,当我添加新项目或编辑项目然后关闭模态控制器时,表格视图会重新加载并多次显示数据。我做错了什么?
这些功能位于呈现视图控制器(我的表视图控制器)中。显然问题必须出在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()
}
此功能位于模态显示的视图控制器中,您可以在其中输入要创建的Parse对象的项目详细信息。
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)
}
})
}
答案 0 :(得分:1)
您似乎正在添加观察者,但不会删除它们。