Skip to content

Instantly share code, notes, and snippets.

@moppymopperson
Forked from watert/UITableView.swift
Created May 13, 2017 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moppymopperson/b54d9118509c25f95c3ad5331a0b528f to your computer and use it in GitHub Desktop.
Save moppymopperson/b54d9118509c25f95c3ad5331a0b528f to your computer and use it in GitHub Desktop.
UITableView example in iOS Playground with XCode 6 beta
// 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