├── MyPlayground.playground
├── contents.xcplayground
├── playground.xcworkspace
│ └── contents.xcworkspacedata
└── Pages
│ └── Generic Table View Controller.xcplaygroundpage
│ ├── timeline.xctimeline
│ └── Contents.swift
└── README.md
/MyPlayground.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Swift Talk
2 | ## Generic Table View Controllers
3 |
4 | This is the code that accompanies Swift Talk Episode 6: [Generic Table View Controllers](https://talk.objc.io/episodes/S01E06-generic-table-view-controllers)
5 |
--------------------------------------------------------------------------------
/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/MyPlayground.playground/Pages/Generic Table View Controller.xcplaygroundpage/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/MyPlayground.playground/Pages/Generic Table View Controller.xcplaygroundpage/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 | import XCPlayground
3 |
4 |
5 | struct Episode {
6 | var title: String
7 | }
8 |
9 | struct Season {
10 | var number: Int
11 | var title: String
12 | }
13 |
14 |
15 | final class ItemsViewController- : UITableViewController {
16 | var items: [Item] = []
17 | let reuseIdentifier = "Cell"
18 | let configure: (Cell, Item) -> ()
19 | var didSelect: (Item) -> () = { _ in }
20 |
21 | init(items: [Item], configure: (Cell, Item) -> ()) {
22 | self.configure = configure
23 | super.init(style: .Plain)
24 | self.items = items
25 | }
26 |
27 | required init?(coder aDecoder: NSCoder) {
28 | fatalError("init(coder:) has not been implemented")
29 | }
30 |
31 | override func viewDidLoad() {
32 | tableView.registerClass(Cell.self, forCellReuseIdentifier: reuseIdentifier)
33 | }
34 |
35 | override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
36 | let item = items[indexPath.row]
37 | didSelect(item)
38 | }
39 |
40 | override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
41 | return items.count
42 | }
43 |
44 | override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
45 | let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! Cell
46 | let item = items[indexPath.row]
47 | configure(cell, item)
48 | return cell
49 | }
50 | }
51 |
52 |
53 | let sampleEpisodes = [
54 | Episode(title: "First Episode"),
55 | Episode(title: "Second Episode"),
56 | Episode(title: "Third Episode")
57 | ]
58 |
59 | let sampleSeasons = [
60 | Season(number: 1, title: "Season One"),
61 | Season(number: 2, title: "Season Two")
62 | ]
63 |
64 |
65 | final class SeasonCell: UITableViewCell {
66 | override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
67 | super.init(style: .Value1, reuseIdentifier: reuseIdentifier)
68 | }
69 |
70 | required init?(coder aDecoder: NSCoder) {
71 | fatalError("init(coder:) has not been implemented")
72 | }
73 | }
74 |
75 |
76 | let seasonsVC = ItemsViewController(items: sampleSeasons, configure: { (cell: SeasonCell, season) in
77 | cell.textLabel?.text = season.title
78 | cell.detailTextLabel?.text = "\(season.number)"
79 | })
80 | seasonsVC.title = "Seasons"
81 |
82 | let nc = UINavigationController(rootViewController: seasonsVC)
83 |
84 | seasonsVC.didSelect = { season in
85 | let episodesVC = ItemsViewController(items: sampleEpisodes, configure: { cell, episode in
86 | cell.textLabel?.text = episode.title
87 | })
88 | episodesVC.title = season.title
89 | nc.pushViewController(episodesVC, animated: true)
90 | }
91 |
92 | nc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
93 | XCPlaygroundPage.currentPage.liveView = nc.view
94 |
95 |
96 |
--------------------------------------------------------------------------------