title: Update a collection view in an other view
body: <p>For school I have to make an iOS application. I would like to use an UICollectionView in an other View. I use the following code, but when I use self.libraryCollectionView.reloadData(), the collectionView function is not called. The CollectionView with the name libraryCollectionView should hold an gallery of Images.
  Here you can see the code I wrote.</p>
  
  <pre><code>import UIKit
  
  class CoverViewController: UIViewController, UICollectionViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource  {
  
  // MARK: Properties
  @IBOutlet weak var photoImageView: UIImageView!
  @IBOutlet weak var typeLabel: UILabel!
  @IBOutlet weak var libraryCollectionView: UICollectionView!
  
  var cover: Cover?
  var images = [UIImage]()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      // Set up views if editing an existing Cover.
      if let cover = cover {
          photoImageView.image = cover.image
          typeLabel.text = cover.type
      }
  
      libraryCollectionView.delegate = self
      libraryCollectionView.dataSource = self
  
      // Do any additional setup after loading the view.
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  /*
  // MARK: - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      // Get the new view controller using segue.destinationViewController.
      // Pass the selected object to the new view controller.
  }
  */
  @IBAction func addImage(_ sender: UIBarButtonItem) {
      let imagePicker = UIImagePickerController()
      imagePicker.delegate = self
  
      let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet);
  
      alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action) in
          if UIImagePickerController.isSourceTypeAvailable(.camera) {
              imagePicker.sourceType = .camera
              self.present(imagePicker, animated: true, completion: nil)
          }
      }))
  
      alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action) in
          if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
              imagePicker.sourceType = .photoLibrary
              self.present(imagePicker, animated: true, completion: nil)
          }
      }))
  
      alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  
      self.present(alert, animated: true, completion: nil)
  }
  
  // Methods for ImagePicker
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
          images.append(pickedImage)
          DispatchQueue.main.async{
              self.libraryCollectionView.reloadData()
          }
      }
  
      dismiss(animated: true, completion: nil)
  }
  
  // Methods for CollectionView
  func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
      return 0
  }
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return images.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cellIdentifier = "cell"
      let cell = libraryCollectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ImageCellView
  
      NSLog(String(images.count))
  
      cell.photoImageView.image = images[indexPath.row]
  
      return cell
  }
  }
  </code></pre>