Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // 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.
- layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) // sets margins(paddings)
- let padding: CGFloat = 10
- layout.itemSize = CGSize(width: (screenWidth / 3) - padding, height: (screenWidth / 3) - padding)
- layout.minimumInteritemSpacing = 10
- layout.minimumLineSpacing = 10
- collectionView.collectionViewLayout = layout
- }
- func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 12
- }
- func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! VendingItemCell
- return cell
- }
- func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: true)
- }
- func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: false)
- }
- func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: true)
- }
- func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
- updateCellBackgroundColor(indexPath, selected: false)
- }
- func updateCellBackgroundColor(indexPath: NSIndexPath, selected: Bool) {
- if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
- 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
- }
Add Comment
Please, Sign In to add comment