Silver Challenge (simple way)


#1

import UIKit

class PhotosViewController: UIViewController, UICollectionViewDelegate /* “, UICollectionViewDelegateFlowLayout” : my add for the Silver Challenge but apparently not necessary */ {

@IBOutlet var collectionView: UICollectionView!

var cellSide: CGFloat!                                                                 // my add for the Silver Challenge

var store: PhotoStore!
let photoDataSource = PhotoDataSource()

override func viewDidLoad() {
    super.viewDidLoad()
    
    collectionView.dataSource = photoDataSource
    collectionView.delegate = self
    
    store.fetchRecentPhotos(){
        (PhotosResult) -> Void in
        
        NSOperationQueue.mainQueue().addOperationWithBlock {
            
            switch PhotosResult {
            case let .Success(photos):
                print("Successfully found  \(photos.count) recent photos.")
                self.photoDataSource.photos = photos
            case let .Failure(error):
                self.photoDataSource.photos.removeAll()
                print("Error fetching recent photos: \(error)")
            }
            self.collectionView.reloadSections(NSIndexSet(index: 0))
        }
    }
}

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    let photo = photoDataSource.photos[indexPath.row]
    
    store.fetchImageForPhoto(photo) {
        (result) -> Void in
        
        NSOperationQueue.mainQueue().addOperationWithBlock() {
            
            let photoIndex = self.photoDataSource.photos.indexOf(photo)!
            let photoIndexPath = NSIndexPath(forRow: photoIndex, inSection: 0)
            
            if let cell = self.collectionView.cellForItemAtIndexPath(photoIndexPath) as? PhotoCollectionViewCell {
                cell.updateWithImage(photo.image)
            }
        }
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowPhoto" {
        if let selectedIndexPath = collectionView.indexPathsForSelectedItems()?.first {
            let photo = photoDataSource.photos[selectedIndexPath.row] // it also seems to work: "let photo = photoDataSource.photos[selectedIndexPath.item]"
            
            let destinationVC = segue.destinationViewController as! PhotoInfoViewController
            destinationVC.photo = photo
            destinationVC.store = store
        }
    }
}

/* ----------------------------------------- my add for the Silver Challenge ----------------------------------------- */

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    
    cellSide = (collectionView.frame.width - 10) / 4
    
    return CGSize(width: cellSide, height: cellSide)
}

override func viewWillTransitionToSize(size: CGSize,
                                       withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
                                                                                                          
    collectionView.collectionViewLayout.invalidateLayout()
}

/* ------------------------------------------------------------------------------------------------------------------- */

}


#2

Not completely correct.

You need PhotosViewController to conform UICollectionViewDelegateFlowLayout to add that sizeForItemAtIndexPath method.