|
9 | 9 | import UIKit
|
10 | 10 | import ReSwift
|
11 | 11 |
|
12 |
| - |
13 | 12 | class ViewController: UIViewController, StoreSubscriber {
|
| 13 | + |
14 | 14 | typealias StoreSubscriberStateType = AppState
|
15 | 15 |
|
16 | 16 | @IBOutlet weak var tableView: UITableView!
|
17 | 17 |
|
18 |
| - |
19 | 18 | override func viewDidLoad() {
|
20 | 19 | super.viewDidLoad()
|
21 |
| - |
22 |
| - // subscribe to state changes |
23 | 20 | mainStore.subscribe(self)
|
24 |
| - } |
| 21 | + } |
25 | 22 |
|
26 | 23 | func newState(state: AppState) {
|
27 |
| - // when the state changes, the UI is updated to reflect the current state |
28 | 24 | tableView.reloadData()
|
29 | 25 | }
|
30 |
| - |
31 |
| - // when either button is tapped, an action is dispatched to the store |
32 |
| - // in order to update the application state |
33 |
| - @IBAction func downTouch(_ sender: AnyObject) { |
34 |
| - mainStore.dispatch(CounterActionDecrease()); |
35 |
| - } |
36 |
| - @IBAction func upTouch(_ sender: AnyObject) { |
37 |
| - mainStore.dispatch(CounterActionIncrease()); |
38 |
| - } |
39 |
| - |
40 | 26 | }
|
41 | 27 |
|
42 | 28 | extension ViewController : UITableViewDelegate {
|
| 29 | + |
43 | 30 | func numberOfSections(in tableView: UITableView) -> Int {
|
44 |
| - return 0 |
| 31 | + return 1 |
45 | 32 | }
|
46 | 33 | }
|
47 | 34 |
|
48 | 35 | extension ViewController : UITableViewDataSource {
|
| 36 | + |
49 | 37 | func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
50 |
| - return 1 |
| 38 | + return mainStore.state.counters.count |
51 | 39 | }
|
| 40 | + |
52 | 41 | func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
53 |
| - //TODO: return actual cell |
54 |
| - return UITableViewCell() |
| 42 | + let cell = tableView.dequeueReusableCell(withIdentifier: "CounterCell", for: indexPath) as! CounterCell |
| 43 | + cell.label.text = "\(mainStore.state.counters[indexPath.row])" |
| 44 | + cell.action = { didIncrease in |
| 45 | + if didIncrease { |
| 46 | + mainStore.dispatch(CounterActionIncrease(index: indexPath.row)) |
| 47 | + } else { |
| 48 | + mainStore.dispatch(CounterActionDecrease(index: indexPath.row)) |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + return cell |
55 | 54 | }
|
56 |
| - |
57 | 55 | }
|
0 commit comments