Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
import PlaygroundSupport
struct Item {
let id: Int
let name: String
}
class ViewController: UITableViewController {
let items: [Item] = [
Item(id: 1, name: "Bulbasaur"),
Item(id: 2, name: "Ivysaur"),
Item(id: 3, name: "Venusaur")
]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "itemTableViewCell")
let item = items[indexPath.row]
cell.textLabel?.text = item.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = items[indexPath.row]
let viewController = ItemViewController(frame: CGRect(x: 0, y: 0, width: 320, height: 480), item: item)
navigationController?.pushViewController(viewController, animated: true)
}
}
class ItemViewController: UIViewController {
private let item: Item
init(frame: CGRect, item: Item) {
self.item = item
super.init(nibName: nil, bundle: nil)
self.title = self.item.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 = ViewController()
rootViewController.title = "Pokemon"
let navigationController = UINavigationController(rootViewController: rootViewController)
navigationController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
PlaygroundPage.current.liveView = navigationController.view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment