Skip to content

Commit 2fea4c4

Browse files
authored
Merge pull request #9 from roberthein/master
Connected the counters to the cell.
2 parents eb3b08c + 71700c5 commit 2fea4c4

File tree

6 files changed

+54
-40
lines changed

6 files changed

+54
-40
lines changed

CounterExample/Actions/Actions.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import ReSwift
22

33
// all of the actions that can be applied to the state
4-
struct CounterActionIncrease: Action {}
5-
struct CounterActionDecrease: Action {}
4+
struct CounterActionIncrease: Action {
5+
let index: Int
6+
}
7+
struct CounterActionDecrease: Action {
8+
let index: Int
9+
}

CounterExample/Base.lproj/Main.storyboard

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@
2525
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
2626
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
2727
<prototypes>
28-
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" rowHeight="68" id="nmG-E3-MuA" customClass="CounterCell" customModule="CounterExample" customModuleProvider="target">
28+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="CounterCell" rowHeight="68" id="nmG-E3-MuA" customClass="CounterCell" customModule="CounterExample" customModuleProvider="target">
2929
<rect key="frame" x="0.0" y="28" width="375" height="68"/>
3030
<autoresizingMask key="autoresizingMask"/>
3131
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nmG-E3-MuA" id="oKv-oX-TLv">
3232
<rect key="frame" x="0.0" y="0.0" width="375" height="67"/>
3333
<autoresizingMask key="autoresizingMask"/>
3434
<subviews>
35-
<stepper opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" maximumValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="pre-xb-w8s">
35+
<stepper opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" autorepeat="NO" minimumValue="-100" maximumValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="pre-xb-w8s">
3636
<rect key="frame" x="273" y="19" width="94" height="29"/>
3737
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
38+
<connections>
39+
<action selector="stepperPressed:" destination="nmG-E3-MuA" eventType="valueChanged" id="TZG-Kz-CkK"/>
40+
</connections>
3841
</stepper>
3942
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rgj-3I-jgW">
4043
<rect key="frame" x="8" y="23" width="42" height="21"/>
@@ -45,6 +48,10 @@
4548
</label>
4649
</subviews>
4750
</tableViewCellContentView>
51+
<connections>
52+
<outlet property="label" destination="rgj-3I-jgW" id="PiX-ed-k3x"/>
53+
<outlet property="stepper" destination="pre-xb-w8s" id="gjH-nk-iU8"/>
54+
</connections>
4855
</tableViewCell>
4956
</prototypes>
5057
<connections>

CounterExample/CounterCell.swift

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
//
88

99
import UIKit
10+
import ReSwift
1011

11-
class CounterCell: UITableViewCell {
12-
13-
override func awakeFromNib() {
14-
super.awakeFromNib()
15-
// Initialization code
16-
}
12+
typealias CellAction = (Bool) -> ()
1713

18-
override func setSelected(_ selected: Bool, animated: Bool) {
19-
super.setSelected(selected, animated: animated)
14+
class CounterCell: UITableViewCell {
15+
16+
var action: CellAction?
2017

21-
// Configure the view for the selected state
18+
@IBOutlet weak var label: UILabel!
19+
@IBOutlet weak var stepper: UIStepper!
20+
21+
@IBAction func stepperPressed(_ sender: UIStepper) {
22+
if sender.value > 0 {
23+
self.action?(true)
24+
} else {
25+
self.action?(false)
26+
}
27+
sender.value = 0
2228
}
23-
2429
}

CounterExample/Reducers/CounterReducer.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ struct CounterReducer: Reducer {
1212
var state = state ?? AppState()
1313

1414
switch action {
15-
case _ as CounterActionIncrease:
16-
state.counters[0] += 1
17-
case _ as CounterActionDecrease:
18-
state.counters[0] -= 1
15+
case let increaseAction as CounterActionIncrease:
16+
state.counters[increaseAction.index] += 1
17+
case let decreaseAction as CounterActionDecrease:
18+
state.counters[decreaseAction.index] -= 1
1919
default:
2020
break
2121
}

CounterExample/State/AppState.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import ReSwift
33
typealias Counter = Int
44

55
struct AppState: StateType {
6-
var counters: [Counter] = []
6+
var counters: [Counter] = [Counter(), Counter(), Counter()]
77
}

CounterExample/ViewController.swift

+19-21
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,47 @@
99
import UIKit
1010
import ReSwift
1111

12-
1312
class ViewController: UIViewController, StoreSubscriber {
13+
1414
typealias StoreSubscriberStateType = AppState
1515

1616
@IBOutlet weak var tableView: UITableView!
1717

18-
1918
override func viewDidLoad() {
2019
super.viewDidLoad()
21-
22-
// subscribe to state changes
2320
mainStore.subscribe(self)
24-
}
21+
}
2522

2623
func newState(state: AppState) {
27-
// when the state changes, the UI is updated to reflect the current state
2824
tableView.reloadData()
2925
}
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-
4026
}
4127

4228
extension ViewController : UITableViewDelegate {
29+
4330
func numberOfSections(in tableView: UITableView) -> Int {
44-
return 0
31+
return 1
4532
}
4633
}
4734

4835
extension ViewController : UITableViewDataSource {
36+
4937
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
50-
return 1
38+
return mainStore.state.counters.count
5139
}
40+
5241
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
5554
}
56-
5755
}

0 commit comments

Comments
 (0)