-
-
Save moppymopperson/b54d9118509c25f95c3ad5331a0b528f to your computer and use it in GitHub Desktop.
UITableView example in iOS Playground with XCode 6 beta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
import PlaygroundSupport | |
struct Pokemon { | |
let id: UInt | |
let name: String | |
} | |
class PokedexViewController: UITableViewController { | |
let pokemons: [Pokemon] = [ | |
Pokemon(id: 1, name: "Bulbasaur"), | |
Pokemon(id: 2, name: "Ivysaur"), | |
Pokemon(id: 3, name: "Venusaur"), | |
Pokemon(id: 4, name: "Charmander"), | |
Pokemon(id: 5, name: "Charmeleon"), | |
Pokemon(id: 6, name: "Charizard"), | |
Pokemon(id: 7, name: "Squirtle"), | |
Pokemon(id: 8, name: "Wartortle"), | |
Pokemon(id: 9, name: "Blastoise") | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return pokemons.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell(style: .default, reuseIdentifier: "PokemonTableViewCell") | |
let pokemon = pokemons[indexPath.row] | |
cell.textLabel?.text = pokemon.name | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
let pokemon = pokemons[indexPath.row] | |
let viewController = PokemonViewController(frame: tableView.frame, pokemon: pokemon) | |
navigationController?.pushViewController(viewController, animated: true) | |
} | |
} | |
class PokemonViewController: UIViewController { | |
private let pokemon: Pokemon | |
init(frame: CGRect, pokemon: Pokemon) { | |
self.pokemon = pokemon | |
super.init(nibName: nil, bundle: nil) | |
self.title = self.pokemon.name | |
self.view = UIView(frame: frame) | |
self.view.backgroundColor = .white | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
let rootViewController = PokedexViewController() | |
rootViewController.title = "Pokedex" | |
let navigationController = UINavigationController(rootViewController: rootViewController) | |
PlaygroundPage.current.liveView = navigationController | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment