title: Creating UICollectionView programmatically
body: <p>I am learning how to create a UICollectionView programmatically. I want to create a grid of pictures collected from the user in another part of the app.
  Will this sample code help me accomplish this? Also, how do I configure the data to emit the image I want? My source code is below.</p>
  
  <p>UICollectionView:</p>
  
  <pre><code>class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
      override func viewDidLoad() {
          super.viewDidLoad()
          let imageStore = ImageStore()
      }
  
      override func viewWillAppear(animated: Bool) {
          super.viewWillAppear(animated)
  
          let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
          layout.itemSize = CGSize(width: 100, height: 100)
  
          let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
          myCollectionView.dataSource = self
          myCollectionView.delegate = self
          myCollectionView.registerClass(RDCellCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
          myCollectionView.backgroundColor = UIColor.whiteColor()
          self.view.addSubview(myCollectionView)
      }
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return images.count
      }
  
      var images: [UIImage] = [
  
      ]
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! RDCellCollectionViewCell
          myCell.imageView.image = images[indexPath.item]
          myCell.backgroundColor = UIColor.grayColor()
          return myCell
      }
  
      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
      {
          print("User tapped on item \(indexPath.row)")
      }
  }
  </code></pre>
  
  <p>ImageStore.swift:</p>
  
  <pre><code>class ImageStore: NSObject {
  
      let cache = NSCache()
  
      func setImage(image: UIImage, forKey key: String) {
          cache.setObject(image, forKey: key)
  
          let imageURL = imageURLForKey(key)
  
          if let data = UIImageJPEGRepresentation(image, 0.5) {
              data.writeToURL(imageURL, atomically: true)
          }
      }
      func imageForKey(key: String) -&gt; UIImage? {
          if let existingImage = cache.objectForKey(key) as? UIImage {
              return existingImage
          }
  
          let imageURL = imageURLForKey(key)
          guard let imageFromDisk = UIImage(contentsOfFile: imageURL.path!) else {
              return nil
          }
  
          cache.setObject(imageFromDisk, forKey: key)
          return imageFromDisk
      }
  
      func deleteImageForKey(key: String) {
          cache.removeObjectForKey(key)
  
          let imageURL = imageURLForKey(key)
          do {
              try NSFileManager.defaultManager().removeItemAtURL(imageURL)
          }
          catch let deleteError {
              print("Error removing the image from disk: \(deleteError)")
          }
      }
  
      func imageURLForKey(key: String) -&gt; NSURL {
          let documentsDirectories =
          NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
          let documentDirectory = documentsDirectories.first!
  
          return documentDirectory.URLByAppendingPathComponent(key)
      }
  }
  </code></pre>