-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathViewController.swift
More file actions
96 lines (65 loc) · 3.38 KB
/
ViewController.swift
File metadata and controls
96 lines (65 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// ViewController.swift
// UICollectionView_p1_Swift
//
// Created by olxios on 20/11/14.
// Copyright (c) 2014 olxios. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
var galleryItems: [GalleryItem] = []
// MARK: -
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
initGalleryItems()
collectionView.reloadData()
}
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
}
// MARK: -
// MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryItems.count
}
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
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
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
}
// MARK: -
// 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)
}
// MARK: -
// 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)
}
}