我有一个表视图,显示来自Parse的对象。我有一个按钮,该按钮以模态显示另一个用于添加新对象的视图控制器。当选择了表格视图中的一行时,它会模态显示一个视图控制器以显示对象信息并允许编辑对象。我遇到的问题是,当我添加新项目或编辑项目然后关闭模式控制器时,表视图将重新加载并多次显示数据。我做错了什么?

这些功能在presenting视图控制器(我的表视图控制器)中。显然,问题必须在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)
            }
        })
    }

最佳答案

看来您正在添加观察者,但并未删除他们。

关于ios - 关闭模态视图 Controller 后多次加载表 View 数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34821853/

10-14 23:20