在UIImagePickerControllerclosures后,UICollectionView将不会reloadData()

我有一个小问题,从UIImagePickerControllerselect图像(并保存到parsing,因为我的UICollectionView从我的图像Parse填充),我的UICollectionView将不会加载新保存的图像,直到我离开该viewController并回到它。 我已经尝试了所有我能想到的工作。 我应该在哪里把我的代码保存和解散控制器?

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { let imagePicture = UIImageJPEGRepresentation(image, 0.1) let imageFile = PFFile(name: "imageFile.jpg", data: image) let saveImage = PFObject(className: "Gallery") saveImage["imageFile"] = imageFile saveImage["dogName"] = self.dogSelectedName saveImage["userID"] = PFUser.currentUser()?.objectId saveImage["dogID"] = self.dogObjectID saveImage.saveInBackgroundWithBlock { (Bool, error) -> Void in if error != nil { print("There was an error") } else { print("New Gallery object saved: \(saveImage)") } } self.collectionView.reloadItemsAtIndexPaths(self.collectionView.indexPathsForVisibleItems()) self.collectionView.reloadData() self.dismissViewControllerAnimated(true) { () -> Void in} } 

在debugging之后,我注意到在视图出现之后(或者根据我写代码的地方,在视图出现之前)我保存了很多对象,并且在我离开并回来之前仍然不会重新加载收集视图。 我已经说了:

 dispatch_async(dispatch_get_main_queue()) { () -> Void in self.collectionView.reloadData() } 

在我的代码中的多个地方,但仍然是相同的结果..我还能怎样设置呢? 任何帮助是很多的帮助! 谢谢。

你的lookUpPhotosfunction很好,但你可以在查询结束时更新collectionView

 func lookUpPhotos() { let findPhotos = PFQuery(className: "Gallery") findPhotos.whereKey("dogID", equalTo: self.dogObjectID) findPhotos.orderByDescending("createdAt") findPhotos.findObjectsInBackgroundWithBlock { (objects, error) -> Void in if let users = objects { for object in users { self.photoData.addObject(object) print("Fetched \(self.dogSelectedName)'s \(self.photoData.count) Images") } } dispatch_async(dispatch_get_main_queue()) { () -> Void in self.collectionView.reloadData() } } } 

您可以在发送数据后再次调用lookUpPhotos函数。

 saveImage.saveInBackgroundWithBlock { (Bool, error) -> Void in if error != nil { print("There was an error") } else { print("New Gallery object saved: \(saveImage)") dispatch_async(dispatch_get_main_queue()) { () -> Void in self.dismissViewControllerAnimated(true) { () -> Void in self.lookUpPhotos() } } } } 

您将其保存在Parse中,但似乎并没有将其添加到数据源中。

所以如果你的UICollectionViewDogDogImage的数据源是一个叫做数组的

var dogArray = []

您需要将您的狗添加到didFinishPickingImage中的数组中

dogArray.append(imagePicture)

希望能帮助到你