Guest User

swfit

a guest
Feb 18th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // VendingMachine
  4. //
  5. // Created by Pasan Premaratne on 1/19/16.
  6. // Copyright © 2016 Treehouse. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. private let reuseIdentifier = "vendingItem"
  12. private let screenWidth = UIScreen.mainScreen().bounds.width
  13.  
  14. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  15.  
  16. @IBOutlet weak var collectionView: UICollectionView!
  17. @IBOutlet weak var totalLabel: UILabel!
  18. @IBOutlet weak var balanceLabel: UILabel!
  19. @IBOutlet weak var quantityLabel: UILabel!
  20.  
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do any additional setup after loading the view, typically from a nib.
  24. setupCollectionViewCells()
  25. }
  26.  
  27. override func didReceiveMemoryWarning() {
  28. super.didReceiveMemoryWarning()
  29. // Dispose of any resources that can be recreated.
  30. }
  31.  
  32. // MARK: - UICollectionView
  33.  
  34. func setupCollectionViewCells() {
  35. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  36. // https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/
  37. // it works with collection view objects to adjust layout information dynamically.
  38.  
  39. layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) // sets margins(paddings)
  40. let padding: CGFloat = 10
  41. layout.itemSize = CGSize(width: (screenWidth / 3) - padding, height: (screenWidth / 3) - padding)
  42. layout.minimumInteritemSpacing = 10
  43. layout.minimumLineSpacing = 10
  44.  
  45. collectionView.collectionViewLayout = layout
  46. }
  47.  
  48. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  49. return 12
  50. }
  51.  
  52. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  53. let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! VendingItemCell
  54.  
  55. return cell
  56. }
  57.  
  58. func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  59. updateCellBackgroundColor(indexPath, selected: true)
  60.  
  61. }
  62.  
  63. func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
  64. updateCellBackgroundColor(indexPath, selected: false)
  65. }
  66.  
  67. func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
  68. updateCellBackgroundColor(indexPath, selected: true)
  69. }
  70.  
  71. func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
  72. updateCellBackgroundColor(indexPath, selected: false)
  73. }
  74.  
  75. func updateCellBackgroundColor(indexPath: NSIndexPath, selected: Bool) {
  76. if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
  77. cell.contentView.backgroundColor = selected ? UIColor(red: 41/255.0, green: 211/255.0, blue: 241/255.0, alpha: 1.0) : UIColor.clearColor()
  78. }
  79. }
  80.  
  81. // MARK: - Helper Methods
  82. }
Add Comment
Please, Sign In to add comment