Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // From Core Text Reference
- // ViewController.swift
- // VendingMachine
- //
- // Created by Pasan Premaratne on 1/19/16.
- // Copyright © 2016 Treehouse. All rights reserved.
- //
- import UIKit
- private let reuseIdentifier = "vendingItem"
- private let screenWidth = UIScreen.mainScreen().bounds.width
- class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
- @IBOutlet weak var collectionView: UICollectionView!
- @IBOutlet weak var totalLabel: UILabel!
- @IBOutlet weak var balanceLabel: UILabel!
- @IBOutlet weak var quantityLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- setupCollectionViewCells()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - UICollectionView
- func setupCollectionViewCells() {
- let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
- // https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/
- // it works with collection view objects to adjust layout information dynamically.
- // you can see objects layout such as size, headers, and footers
- // found in UIKit Framework Reference
- layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) // sets margins(paddings)
- let padding: CGFloat = 10 //specialized float holds 32 bit or 64 part of Core Graphics
- //Core Graphics Framework Reference
- //https://developer.apple.com/library/tvos/documentation/CoreGraphics/Reference/CoreGraphics_Framework/index.html
- layout.itemSize = CGSize(width: (screenWidth / 3) - padding, height: (screenWidth / 3) - padding)
- //struct CGSize { var width: CGFloat var height: CGFloat init() init(width width: CGFloat, height height: CGFloat) }
- // 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
- layout.minimumInteritemSpacing = 10 // minimum spacing between items in same row. var minimumInteritemSpacing: CG Float
- layout.minimumLineSpacing = 10 // minimum spacing between lines of items in grid. . var minimumLineSpacing: CG Float
- collectionView.collectionViewLayout = layout // var collectionView: UICollectionView? {get}, collection view object
- //using this layout object, collection view object sets value of this property when new layout object is assigned to it.
- // I don’t know collectionViewLayout
- }
- func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 12 // UICollectionView takes ordered collection of data items and presents them using customizable layouts.
- // numberOfItemsInSection is all about State of Collection View, returns number of items in specific section
- // fund numberOfItemsInSection(_ section: Int) -> Int // section|index of section you want to count items.
- }
- func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
- // locating items and views in collection view fund cellForItemAtIndexPath(_ indexPath: NSIndexPath) -> UICollectionViewCell?
- // indexPath| The index path that specifies the section and item number of the cell
- UICollectionViewCell {
- let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! VendingItemCell
- // func dequeueReusableCellWithReuseIdentifier(_ identifier: String, forIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
- // identifier | reuse identifier for the specified cell. This parameter must not be nil.
- // indexPath | index path specifying location of cell. data source receives this information when it is asked
- // for the cell and should just pass it along. This method uses the index path to perform additional
- // configuration view.
- // return value is a UICollectionReusableView
- return cell
- }
- func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: true)
- // this is under Declaration, Parameters or Managing the Selected Cells.
- // optional fund collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
- // collectionView | collection view object that is notifying you of the selection change.
- // indexPath | The index path of the cell that was selected.
- }
- func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
- // didDeselectItemAtIndexPath,
- //optional func collectionView(_collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
- // so the api is exactly the same as this example code.
- updateCellBackgroundColor(indexPath, selected: false) // not sure where this fits in.
- }
- func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: true)
- // Under Managing Cell Highlighting
- }
- func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: false)
- } // exactly same as API in UIKit Framework Reference except update..blah is plugged in. below is where it really shines.
- func updateCellBackgroundColor(indexPath: NSIndexPath, selected: Bool) {
- if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
- // Getting Views for Items, cellForItemAtIndexPath asks data source object for the
- // cell that corresponds to the specified item in the collection view.
- // not same as API below
- // func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
- // notice above collection view is bound to a variable so becomes an attribute or
- // stored property? so cellForItemAtIndexPath was a extension name to a parameter to
- // collectionView func
- // an then is stored to variable calls its own parameter.
- // collectionView.cellForItemAtIndexPath(indexPath) so it must call NSIndexPath?
- // so What is NSIndexPath, it was set to true at one point
- // The NSIndexPath class represents the path to a specific node in a tree of nested array
- // collections. This path is known as an index path. Nested arrays create paths to different arrays.
- //Foundation framework reference to facilitate the identification of rows and sections in
- // “UITableView” objects and the identification of items and sections in “UICollectionView” objects.
- cell.contentView.backgroundColor = selected ? UIColor(red: 41/255.0, green: 211/255.0, blue: 241/255.0, alpha: 1.0) : UIColor.clearColor()
- }
- }
- // MARK: - Helper Methods
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement