Skip to content

Instantly share code, notes, and snippets.

@bgayman
Created February 14, 2016 03:31
Show Gist options
  • Save bgayman/6dbd364e46030ebd2714 to your computer and use it in GitHub Desktop.
Save bgayman/6dbd364e46030ebd2714 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
var str = "Hello, playground"
private let reuseIdentifier = "Cell"
class NumberCell: UICollectionViewCell {
let numberLabel = UILabel()
override var selected: Bool {
didSet {
// Updates as expected
contentView.backgroundColor = self.selected ? UIColor.redColor() : UIColor.clearColor()
}
}
var number: Int? {
didSet {
// Sets and shows the text in the number label as expected when cell is first initialised
if let number = number {
numberLabel.text = String(number)
}
}
}
var isCrossedOut: Bool = false {
didSet {
// Sets and displays correct values on initialisation, but later
// stops updating display
contentView.backgroundColor = self.isCrossedOut ? UIColor.blackColor() : UIColor.clearColor()
}
}
// ...
}
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
collectionView!.registerClass(NumberCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 2
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NumberCell
cell.backgroundColor = UIColor.whiteColor()
// Configure the cell
return cell
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
}
*/
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}
@IBAction func crossOut(sender: AnyObject) {
let cell = self.collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! NumberCell
cell.isCrossedOut = !cell.isCrossedOut
//cell.setNeedsDisplay()
}
}
let collectionViewController = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
XCPlaygroundPage.currentPage.liveView = collectionViewController.collectionView
collectionViewController.crossOut(collectionViewController)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment