Skip to content

Instantly share code, notes, and snippets.

@snit-ram
Created February 10, 2016 01:09
Show Gist options
  • Save snit-ram/e2b20cb696f62f969221 to your computer and use it in GitHub Desktop.
Save snit-ram/e2b20cb696f62f969221 to your computer and use it in GitHub Desktop.
collectionview.swift
//
// ViewController.swift
// sss
//
// Created by Rafael Martins on 2/9/16.
// Copyright © 2016 Yahoo Inc. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var nextButton: UIButton!
let buttonLabels = ["a", "b", "c", "d", "e", "f"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.allowsMultipleSelection = true
changeButton()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonLabels.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BubbleCell
cell.titleLabel.text = buttonLabels[indexPath.item]
return cell
}
func changeButton(){
nextButton.hidden = (collectionView.indexPathsForSelectedItems()?.isEmpty ?? true)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
changeButton()
collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.redColor()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
changeButton()
collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
}
@IBAction func nexTap(sender: AnyObject) {
performSegueWithIdentifier("goToNextScreen", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToNextScreen" {
let selectedButtons = collectionView.indexPathsForSelectedItems()?.map {
self.buttonLabels[$0.item]
}
let tableVC = segue.destinationViewController as! EventTableViewController
tableVC.eventTypes = selectedButtons!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment