关闭先前的VC后仅重新加载新数据

时间:2019-12-10 20:14:45

标签: ios swift uitableview parsing uirefreshcontrol

所以我有一个Photo VC,可以在其中发布如下所述的图像和标题:

@IBAction func postPressed(_ sender: Any) {
    self.view.isUserInteractionEnabled = false
    if imageOutlet.imageView?.image != UIImage(named: "plus_photo") && postImageTextField.text.count >= 5 && postImageTextField.text != "Write a caption..." {
        var userProfileImageData = Data()
        let query = PFQuery(className: "_User")
        query.whereKey("username", equalTo: PFUser.current()?.username)
        query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
        if let objects = objects {
            for object in objects {
                let userImageFile = object["profilePic"] as? PFFileObject
                userImageFile?.getDataInBackground(block: { (userProfileImageData1: Data?, error: Error?) in
                    if error == nil {
                        userProfileImageData = userProfileImageData1!
                    let currentDateTime = Date()
                                   let formatter = DateFormatter()
                                   formatter.timeStyle = .short
                                   formatter.dateStyle = .none
                                   let captionData = self.postImageTextField.text
                                   let userPhoto = PFObject(className: "PicPost")
                                let imageData = self.imageOutlet.imageView?.image?.jpegData(compressionQuality: 0.1)!
                                   let imageFile = PFFileObject(name: "image.png", data: imageData!)
                                    let imageFile2 = PFFileObject(name: "image.png", data: userProfileImageData)


                        userPhoto["ProfilePhoto"] = imageFile2
                        userPhoto["Bar"] = self.barText
                        userPhoto["ImageFile"] = imageFile
                        userPhoto["UserCaption"] = captionData
                        userPhoto["UserLikes"] = 0
                        userPhoto["UserTime"] = (formatter.string(from: currentDateTime))
                        userPhoto["Username"] = PFUser.current()?.username
                        userPhoto.saveInBackground { (success, error) in
                            if success {
                            self.postImageTextField.resignFirstResponder()
                            //Go back to original VC
                            self.presentingViewController?.dismiss(animated: true, completion: nil)
                            } else {
                                print(error)
                                self.view.isUserInteractionEnabled = true

                            }
                        }


                    } else {
                        print(error)
                    }
                })
            }
        }
    })
}

}

我使用以下行关闭该VC:

            self.presentingViewController?.dismiss(animated: true, completion: nil)

此新的tableView VC已加载数据:

    func getPicData() {
    let query = PFQuery(className: "PicPost")
    query.whereKey("Bar", equalTo: BarText!)
    query.addDescendingOrder("createdAt")
    query.limit = picLimit
    query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
        if let objects = objects {
            for object in objects {
                self.picsLoaded = true
                let ImageFile = object["ImageFile"] as? PFFileObject
                let urlString = ImageFile?.url as! String
                if let url = URL(string: urlString) {
                    let data = try? Data(contentsOf: url)
                    if let imageData = data {
                        self.picPostPicUrlArray.append(urlString as NSString)
                        self.picPostPicImageCache.setObject(UIImage(data:imageData)!,forKey: urlString as NSString)
                    }
                }
                let ProfileImageFile = object["ProfilePhoto"] as? PFFileObject
                let urlString2 = ProfileImageFile?.url as! String
                if let url2 = URL(string: urlString2) {
                    let data = try? Data(contentsOf: url2)
                    if let imageData = data {
                        self.picPostProPicUrlArray.append(urlString2 as NSString)
                        self.picPostProPicImageCache.setObject(UIImage(data: imageData)!, forKey: urlString2 as NSString)
                    }
                }
                if object["UserLikes"] != nil && object["Username"] != nil && object["UserTime"] != nil {
                self.picLikeArray.append(object["UserLikes"] as! Int)
                self.picUsernameArray.append(object["Username"] as! String)
                self.picTimeArray.append(object["UserTime"] as! String)
                self.picObjectIdArray.append(object.objectId!)
                self.picCaptionArray.append(object["UserCaption"] as! String)

                }
            }
            self.Reload()
        }
    })
}

如您所见,函数getPicData()已经从parse.com加载了所有数据。它存储在各种数组中。

我想做的是仅加载新数据并将其添加到当前tableView中。我试图做的是从PhotoVC到tableView VC。然后,新的segue将重新加载所有data()。不幸的是,这对我的记忆非常不利。

我需要解决的两个问题

1)仅加载新数据并将其加载到当前表视图 2)关闭tableView VC返回主屏幕。现在,它撤消了它刚发布的版本,即VC。

谢谢

1 个答案:

答案 0 :(得分:2)

对于将新数据添加到现有可用阵列中的第一个问题。您可以在PhotoVC中使用委托方法将新发布的对象传递到以前的控制器,即在您的案例tableView VC中。这是两个类之间的委派代码片段。

protocol PhotoVCdelegate : class{
   func postAdded(item : PFObject)    
}

class PhotoVC{
   weak var delegate : PhotoVCdelegate?

   @IBAction func postPressed(_ sender: Any) {
    self.view.isUserInteractionEnabled = false
if imageOutlet.imageView?.image != UIImage(named: "plus_photo") && postImageTextField.text.count >= 5 && postImageTextField.text != "Write a caption..." {
    var userProfileImageData = Data()
    let query = PFQuery(className: "_User")
    query.whereKey("username", equalTo: PFUser.current()?.username)
    query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
    if let objects = objects {
        for object in objects {
            let userImageFile = object["profilePic"] as? PFFileObject
            userImageFile?.getDataInBackground(block: { (userProfileImageData1: Data?, error: Error?) in
                if error == nil {
                    userProfileImageData = userProfileImageData1!
                let currentDateTime = Date()
                               let formatter = DateFormatter()
                               formatter.timeStyle = .short
                               formatter.dateStyle = .none
                               let captionData = self.postImageTextField.text
                               let userPhoto = PFObject(className: "PicPost")
                            let imageData = self.imageOutlet.imageView?.image?.jpegData(compressionQuality: 0.1)!
                               let imageFile = PFFileObject(name: "image.png", data: imageData!)
                                let imageFile2 = PFFileObject(name: "image.png", data: userProfileImageData)


                    userPhoto["ProfilePhoto"] = imageFile2
                    userPhoto["Bar"] = self.barText
                    userPhoto["ImageFile"] = imageFile
                    userPhoto["UserCaption"] = captionData
                    userPhoto["UserLikes"] = 0
                    userPhoto["UserTime"] = (formatter.string(from: currentDateTime))
                    userPhoto["Username"] = PFUser.current()?.username
                    userPhoto.saveInBackground { (success, error) in
                        if success {
                        self.postImageTextField.resignFirstResponder()
                        //Go back to original VC

                       //HERE you can pass this information to your controller
                        self.delegate?.postAdded(item : userPhoto)


                        self.presentingViewController?.dismiss(animated: true, completion: nil)
                        } else {
                            print(error)
                            self.view.isUserInteractionEnabled = true

                        }
                    }


                } else {
                    print(error)
                }
            })
        }
    }
})
   }
}

在您的TableViewVC.swift中 假设您具有在单击按钮时显示控制器的功能。

class TableViewVC{ 

    @IBAction func createPostBtnPressed(_ sender: Any) {
       if let vc =    self.storyboard?.instantiateViewController(withIdentifier:"PhotoVC") as? PhotoVC{
           vc.delegate = self
           self.presentViewController(vc, animated: true, completion: nil)
         }
     }

}
成功发布数据后,将调用PhotoVC的

delegate方法。

extension TableViewVC : PhotoVCdelegate{ 
    func postAdded(item : PFObject){ 
          // append required value from item object into your arrays and reload tableview . 

     }
}

希望这会有所帮助!