Skip to content

Instantly share code, notes, and snippets.

@digoreis
Last active August 1, 2016 01:31
Show Gist options
  • Save digoreis/ab8bdb8495a8052cd2488bc3c7afc03b to your computer and use it in GitHub Desktop.
Save digoreis/ab8bdb8495a8052cd2488bc3c7afc03b to your computer and use it in GitHub Desktop.
Group Values with Enum
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
enum CellData<A, B, C> {
case Number(A)
case Name(B)
case Money(C)
func title() -> String {
switch self {
case .Number(let number):
return "Number : \(number)"
case .Name(let name):
return "Name : \(name)"
case .Money(let money):
return "Money : \(money)"
}
}
}
final class MyTableViewController : UITableViewController {
let data : [CellData<Int,String,Double>] =
[CellData.Number(1),
CellData.Money(10.2),
CellData.Number(2),
CellData.Name("My name"),
CellData.Money(5.99),
CellData.Name("My name2")]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = data[indexPath.row].title()
return cell
}
}
let tableViewController = MyTableViewController()
XCPlaygroundPage.currentPage.liveView = tableViewController.view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment