Skip to content

Instantly share code, notes, and snippets.

@olxios
Last active November 23, 2016 13:27
Show Gist options
  • Save olxios/119aacd77a12b7bb68e76ae6deeafe78 to your computer and use it in GitHub Desktop.
Save olxios/119aacd77a12b7bb68e76ae6deeafe78 to your computer and use it in GitHub Desktop.
import Foundation
class GalleryItem {
var itemImage: String
init(dataDictionary:Dictionary<String,String>) {
itemImage = dataDictionary["itemImage"]!
}
class func newGalleryItem(_ dataDictionary:Dictionary<String,String>) -> GalleryItem {
return GalleryItem(dataDictionary: dataDictionary)
}
}
import UIKit
class GalleryItemCollectionViewCell: UICollectionViewCell {
@IBOutlet var itemImageView: UIImageView!
}
import UIKit
class GalleryItemCollectionViewCell: UICollectionViewCell {
@IBOutlet var itemImageView: UIImageView!
func setGalleryItem(_ item:GalleryItem) {
itemImageView.image = UIImage(named: item.itemImage)
}
}
import UIKit
class GalleryItemCommentView: UICollectionReusableView {
@IBOutlet var commentLabel: UILabel!
}
import UIKit
class ViewController: UIViewController {
@IBOutlet var collectionView: UICollectionView!
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
...
// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
// MARK: - UICollectionViewDataSource
...
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryItems.count
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
// MARK: - UICollectionViewDataSource
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryItemCollectionViewCell", for: indexPath) as! GalleryItemCollectionViewCell
cell.setGalleryItem(galleryItems[indexPath.row])
return cell
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
// MARK: - UICollectionViewDataSource
...
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let commentView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "GalleryItemCommentView", for: indexPath) as! GalleryItemCommentView
commentView.commentLabel.text = "Supplementary view of kind \(kind)"
return commentView
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
...
// MARK: - UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let alert = UIAlertController(title: "didSelectItemAtIndexPath:", message: "Indexpath = \(indexPath)", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Dismiss", style: .destructive, handler: nil)
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
....
var galleryItems: [GalleryItem] = [] // NEW PROPERTY
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
...
// MARK: - UICollectionViewFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let picDimension = self.view.frame.size.width / 4.0
return CGSize(width: picDimension, height: picDimension)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let leftRightInset = self.view.frame.size.width / 14.0
return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset)
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
....
fileprivate func initGalleryItems() {
var items = [GalleryItem]()
let inputFile = Bundle.main.path(forResource: "items", ofType: "plist")
let inputDataArray = NSArray(contentsOfFile: inputFile!)
for inputItem in inputDataArray as! [Dictionary<String, String>] {
let galleryItem = GalleryItem(dataDictionary: inputItem)
items.append(galleryItem)
}
galleryItems = items
}
}
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
...
override func viewDidLoad() {
super.viewDidLoad()
initGalleryItems()
collectionView.reloadData()
}
private func initGalleryItems() {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment