Skip to content

Instantly share code, notes, and snippets.

@vjosullivan
Last active August 29, 2015 14:18
Show Gist options
  • Save vjosullivan/5cec609803a2ef5f06b2 to your computer and use it in GitHub Desktop.
Save vjosullivan/5cec609803a2ef5f06b2 to your computer and use it in GitHub Desktop.
//
// TableViewController.swift
// ColouredRose2
//
// Created by Vincent O'Sullivan on 08/04/2015.
// Copyright (c) 2015 Vincent O'Sullivan. All rights reserved.
//
import UIKit
class TableViewController: UITableViewController {
let maxSections = 8
let maxRows = 10
// MARK: - TableView functions
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return maxSections
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return maxRows
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = "Section \(indexPath.section), row \(indexPath.row)."
cell.backgroundColor = colourForIndexPath(indexPath)
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)."
}
// MARK: - Internal functions
private func colourForIndexPath(indexPath:NSIndexPath) -> UIColor {
let row = CGFloat(indexPath.row)
let section = CGFloat(indexPath.section)
let saturation = 1.0 - row / CGFloat(maxRows)
let hue = section / CGFloat(maxSections)
return UIColor(hue: hue, saturation: saturation, brightness: 1.0, alpha: 1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment