Advertisement
Guest User

Vending maching

a guest
Feb 18th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. // From Core Text Reference
  2.  
  3. // ViewController.swift
  4. // VendingMachine
  5. //
  6. // Created by Pasan Premaratne on 1/19/16.
  7. // Copyright © 2016 Treehouse. All rights reserved.
  8. //
  9.  
  10. import UIKit
  11.  
  12. private let reuseIdentifier = "vendingItem"
  13. private let screenWidth = UIScreen.mainScreen().bounds.width
  14.  
  15. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  16.  
  17. @IBOutlet weak var collectionView: UICollectionView!
  18. @IBOutlet weak var totalLabel: UILabel!
  19. @IBOutlet weak var balanceLabel: UILabel!
  20. @IBOutlet weak var quantityLabel: UILabel!
  21.  
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do any additional setup after loading the view, typically from a nib.
  25. setupCollectionViewCells()
  26. }
  27.  
  28. override func didReceiveMemoryWarning() {
  29. super.didReceiveMemoryWarning()
  30. // Dispose of any resources that can be recreated.
  31. }
  32.  
  33. // MARK: - UICollectionView
  34.  
  35. func setupCollectionViewCells() {
  36. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  37. // https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/
  38. // it works with collection view objects to adjust layout information dynamically.
  39. // you can see objects layout such as size, headers, and footers
  40. // found in UIKit Framework Reference
  41.  
  42. layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) // sets margins(paddings)
  43. let padding: CGFloat = 10 //specialized float holds 32 bit or 64 part of Core Graphics
  44. //Core Graphics Framework Reference
  45. //https://developer.apple.com/library/tvos/documentation/CoreGraphics/Reference/CoreGraphics_Framework/index.html
  46.  
  47. layout.itemSize = CGSize(width: (screenWidth / 3) - padding, height: (screenWidth / 3) - padding)
  48. //struct CGSize { var width: CGFloat var height: CGFloat init() init(width width: CGFloat, height height: CGFloat) }
  49. // so as you can see it is a struct, and when the struct is called it is in form CGSize(param: value) like all structs that get called
  50.  
  51. layout.minimumInteritemSpacing = 10 // minimum spacing between items in same row. var minimumInteritemSpacing: CG Float
  52. layout.minimumLineSpacing = 10 // minimum spacing between lines of items in grid. . var minimumLineSpacing: CG Float
  53.  
  54. collectionView.collectionViewLayout = layout // var collectionView: UICollectionView? {get}, collection view object
  55. //using this layout object, collection view object sets value of this property when new layout object is assigned to it.
  56. // I don’t know collectionViewLayout
  57. }
  58.  
  59. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  60. return 12 // UICollectionView takes ordered collection of data items and presents them using customizable layouts.
  61. // numberOfItemsInSection is all about State of Collection View, returns number of items in specific section
  62. // fund numberOfItemsInSection(_ section: Int) -> Int // section|index of section you want to count items.
  63. }
  64.  
  65. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
  66. // locating items and views in collection view fund cellForItemAtIndexPath(_ indexPath: NSIndexPath) -> UICollectionViewCell?
  67. // indexPath| The index path that specifies the section and item number of the cell
  68.  
  69.  
  70. UICollectionViewCell {
  71. let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! VendingItemCell
  72. // func dequeueReusableCellWithReuseIdentifier(_ identifier: String, forIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
  73.  
  74. // identifier | reuse identifier for the specified cell. This parameter must not be nil.
  75. // indexPath | index path specifying location of cell. data source receives this information when it is asked
  76. // for the cell and should just pass it along. This method uses the index path to perform additional
  77. // configuration view.
  78. // return value is a UICollectionReusableView
  79.  
  80. return cell
  81. }
  82.  
  83. func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  84. updateCellBackgroundColor(indexPath, selected: true)
  85.  
  86. // this is under Declaration, Parameters or Managing the Selected Cells.
  87.  
  88. // optional fund collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
  89.  
  90. // collectionView | collection view object that is notifying you of the selection change.
  91. // indexPath | The index path of the cell that was selected.
  92.  
  93. }
  94.  
  95. func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
  96.  
  97. // didDeselectItemAtIndexPath,
  98.  
  99. //optional func collectionView(_collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
  100.  
  101. // so the api is exactly the same as this example code.
  102.  
  103. updateCellBackgroundColor(indexPath, selected: false) // not sure where this fits in.
  104. }
  105.  
  106. func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
  107. updateCellBackgroundColor(indexPath, selected: true)
  108. // Under Managing Cell Highlighting
  109.  
  110. }
  111.  
  112. func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
  113. updateCellBackgroundColor(indexPath, selected: false)
  114. } // exactly same as API in UIKit Framework Reference except update..blah is plugged in. below is where it really shines.
  115.  
  116. func updateCellBackgroundColor(indexPath: NSIndexPath, selected: Bool) {
  117. if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
  118.  
  119. // Getting Views for Items, cellForItemAtIndexPath asks data source object for the
  120. // cell that corresponds to the specified item in the collection view.
  121. // not same as API below
  122.  
  123. // func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
  124.  
  125. // notice above collection view is bound to a variable so becomes an attribute or
  126. // stored property? so cellForItemAtIndexPath was a extension name to a parameter to
  127. // collectionView func
  128. // an then is stored to variable calls its own parameter.
  129. // collectionView.cellForItemAtIndexPath(indexPath) so it must call NSIndexPath?
  130. // so What is NSIndexPath, it was set to true at one point
  131. // The NSIndexPath class represents the path to a specific node in a tree of nested array
  132. // collections. This path is known as an index path. Nested arrays create paths to different arrays.
  133.  
  134. //Foundation framework reference to facilitate the identification of rows and sections in
  135. // “UITableView” objects and the identification of items and sections in “UICollectionView” objects.
  136.  
  137. cell.contentView.backgroundColor = selected ? UIColor(red: 41/255.0, green: 211/255.0, blue: 241/255.0, alpha: 1.0) : UIColor.clearColor()
  138. }
  139. }
  140.  
  141. // MARK: - Helper Methods
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement