Skip to content

Instantly share code, notes, and snippets.

@naoty
Last active April 6, 2018 23:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save naoty/c2de611d3260df12bc45 to your computer and use it in GitHub Desktop.
Save naoty/c2de611d3260df12bc45 to your computer and use it in GitHub Desktop.
A playground to display a table view controller on a navigation controller
import XCPlayground
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 numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pokemons.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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, didSelectRowAtIndexPath indexPath: NSIndexPath) {
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.whiteColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let rootViewController = PokedexViewController()
rootViewController.title = "Pokedex"
let navigationController = UINavigationController(rootViewController: rootViewController)
XCPlaygroundPage.currentPage.liveView = navigationController.view
@jeremy6462
Copy link

This was super helpful! I forked and updated to use the new PageSupport framework and Swift 3, since I wasn't able to compile in Xcode 8. Here's my fork if you wish to update this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment