-
-
Save amadeu01/2f1b95b7471b5fe9187483fe2860bf72 to your computer and use it in GitHub Desktop.
A playground to display a table view controller on a navigation controller
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
import PlaygroundSupport | |
import UIKit | |
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 = UIColor.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.view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment