Skip to content

Instantly share code, notes, and snippets.

Created August 23, 2014 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5c22cc92c81cc5912d91 to your computer and use it in GitHub Desktop.
Save anonymous/5c22cc92c81cc5912d91 to your computer and use it in GitHub Desktop.
import UIKit
import QuartzCore
import Foundation
class StocksTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let gradientGreen = Colors().greenGradient()
private let stocks: [(String, Double)] = [("AAPL", 1.5), ("TSLA", -2.5), ("GOOG", 2.5) ]
let cellSize:CGFloat = 120.0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return stocks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cellID")
cell.textLabel.text = stocks[indexPath.row].0 // Position 0 of the tuple, AAPL in this case
cell.detailTextLabel.text = "\(stocks[indexPath.row].1)" + "%"
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
NSLog("\(stocks[indexPath.row].0)")
}
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
switch stocks[indexPath.row].1 {
case let x where x > 0.0: // Positive %
gradientGreen.frame = cell.contentView.bounds
var view = UIView(frame: cell.bounds)
cell.backgroundView = view
cell.contentView.layer.insertSublayer(self.gradientGreen, atIndex: 0)
case let x where x < 0.0: // Negative %
// rgb(252,61,57)
cell.backgroundColor = UIColor(red: 252.0/255.0, green: 61/255.0, blue: 57.0/255.0, alpha: 1.0)
case let x:
cell.backgroundColor = UIColor.purpleColor()
}
cell.textLabel.textColor = UIColor.whiteColor()
cell.detailTextLabel.textColor = UIColor.whiteColor()
cell.textLabel.font = UIFont(name: "HelveticaNeue", size: 48)
cell.detailTextLabel.font = UIFont(name: "HelveticaNeue", size: 24)
cell.textLabel.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25)
cell.textLabel.shadowOffset = CGSize(width: 0, height: 1)
cell.detailTextLabel.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25)
cell.detailTextLabel.shadowOffset = CGSize(width: 0, height: 1)
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return cellSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment