Skip to content

Commit f4a3694

Browse files
committed
SwitchTableViewCell: Standard Subtitle UITableViewCell + Switch
1 parent aacb066 commit f4a3694

File tree

2 files changed

+79
-61
lines changed

2 files changed

+79
-61
lines changed
Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
11
import UIKit
22

3+
4+
/// UITableViewCell (Subtitle Style) with a UISwitch on its right hand side.
5+
///
36
class SwitchTableViewCell: UITableViewCell {
47

5-
// MARK: - Properties
6-
//
7-
@IBOutlet public var topLabel: UILabel!
8-
@IBOutlet public var bottomLabel: UILabel!
9-
@IBOutlet public var toggleSwitch: UISwitch!
8+
/// Toggle Switch
9+
///
10+
private let toggleSwitch = UISwitch()
11+
12+
/// Closure to be executed whenever the Switch is flipped
13+
///
14+
var onChange: ((Bool) -> Void)?
1015

11-
var onToggleSwitchTouchUp: (() -> Void)?
16+
/// Text: Title
17+
///
18+
var title: String? {
19+
get {
20+
return textLabel?.text
21+
}
22+
set {
23+
textLabel?.text = newValue
24+
}
25+
}
26+
27+
/// Text: Subtitle
28+
///
29+
var subtitle: String? {
30+
get {
31+
return detailTextLabel?.text
32+
}
33+
set {
34+
detailTextLabel?.text = newValue
35+
}
36+
}
37+
38+
/// Boolean indicating if the Switch is On or Off.
39+
///
40+
var isOn: Bool {
41+
get {
42+
return toggleSwitch.isOn
43+
}
44+
set {
45+
toggleSwitch.isOn = newValue
46+
}
47+
}
48+
49+
// MARK: - Overridden Methods
1250

1351
override func awakeFromNib() {
1452
super.awakeFromNib()
1553

16-
topLabel.applyBodyStyle()
17-
bottomLabel.applyFootnoteStyle()
18-
toggleSwitch.onTintColor = StyleManager.wooCommerceBrandColor
54+
setupTextLabels()
55+
setupSwitch()
1956
setupGestureRecognizers()
2057
}
2158

2259
@IBAction func toggleSwitchWasPressed() {
23-
onToggleSwitchTouchUp?()
60+
onChange?(toggleSwitch.isOn)
2461
}
2562
}
2663

@@ -29,6 +66,20 @@ class SwitchTableViewCell: UITableViewCell {
2966
//
3067
private extension SwitchTableViewCell {
3168

69+
func setupTextLabels() {
70+
textLabel?.text = String()
71+
textLabel?.applyBodyStyle()
72+
73+
detailTextLabel?.text = String()
74+
detailTextLabel?.applyFootnoteStyle()
75+
}
76+
77+
func setupSwitch() {
78+
toggleSwitch.onTintColor = StyleManager.wooCommerceBrandColor
79+
toggleSwitch.addTarget(self, action: #selector(toggleSwitchWasPressed), for: .touchUpInside)
80+
accessoryView = toggleSwitch
81+
}
82+
3283
func setupGestureRecognizers() {
3384
let gestureRecognizer = UITapGestureRecognizer()
3485
gestureRecognizer.on { [weak self] gesture in
@@ -39,7 +90,8 @@ private extension SwitchTableViewCell {
3990
}
4091

4192
func contentViewWasPressed() {
42-
toggleSwitch.isOn = !toggleSwitch.isOn
43-
onToggleSwitchTouchUp?()
93+
let newValue = !toggleSwitch.isOn
94+
toggleSwitch.setOn(newValue, animated: true)
95+
onChange?(newValue)
4496
}
4597
}

WooCommerce/Classes/ViewRelated/ReusableViews/SwitchTableViewCell.xib

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,30 @@
1212
<objects>
1313
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
1414
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
15-
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" rowHeight="75" id="KGk-i7-Jjw" customClass="SwitchTableViewCell" customModule="WooCommerce" customModuleProvider="target">
15+
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" textLabel="Sig-bv-PUS" detailTextLabel="yEO-uk-ng6" rowHeight="75" style="IBUITableViewCellStyleSubtitle" id="KGk-i7-Jjw" customClass="SwitchTableViewCell" customModule="WooCommerce" customModuleProvider="target">
1616
<rect key="frame" x="0.0" y="0.0" width="320" height="75"/>
1717
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1818
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
1919
<rect key="frame" x="0.0" y="0.0" width="320" height="74.5"/>
2020
<autoresizingMask key="autoresizingMask"/>
2121
<subviews>
22-
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="wbD-Qx-IG5">
23-
<rect key="frame" x="16" y="16" width="223" height="43"/>
24-
<subviews>
25-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="B98-58-fIv">
26-
<rect key="frame" x="0.0" y="0.0" width="223" height="20.5"/>
27-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
28-
<nil key="textColor"/>
29-
<nil key="highlightedColor"/>
30-
</label>
31-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hrx-8V-5Ga">
32-
<rect key="frame" x="0.0" y="20.5" width="223" height="22.5"/>
33-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
34-
<nil key="textColor"/>
35-
<nil key="highlightedColor"/>
36-
</label>
37-
</subviews>
38-
</stackView>
39-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SdQ-xn-v14">
40-
<rect key="frame" x="239" y="0.0" width="81" height="74.5"/>
41-
<subviews>
42-
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="eQm-bW-led">
43-
<rect key="frame" x="16" y="22" width="51" height="31"/>
44-
<connections>
45-
<action selector="toggleSwitchWasPressed" destination="KGk-i7-Jjw" eventType="touchUpInside" id="beq-E0-PHE"/>
46-
</connections>
47-
</switch>
48-
</subviews>
49-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
50-
<constraints>
51-
<constraint firstItem="eQm-bW-led" firstAttribute="centerY" secondItem="SdQ-xn-v14" secondAttribute="centerY" id="5eK-cD-Y34"/>
52-
<constraint firstAttribute="trailingMargin" secondItem="eQm-bW-led" secondAttribute="trailing" constant="8" id="JHu-WR-UIl"/>
53-
<constraint firstAttribute="width" constant="81" id="qba-K1-uAZ"/>
54-
</constraints>
55-
</view>
22+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Sig-bv-PUS">
23+
<rect key="frame" x="16" y="17" width="33.5" height="20.5"/>
24+
<autoresizingMask key="autoresizingMask"/>
25+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
26+
<nil key="textColor"/>
27+
<nil key="highlightedColor"/>
28+
</label>
29+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="yEO-uk-ng6">
30+
<rect key="frame" x="16" y="40.5" width="44" height="14.5"/>
31+
<autoresizingMask key="autoresizingMask"/>
32+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
33+
<nil key="textColor"/>
34+
<nil key="highlightedColor"/>
35+
</label>
5636
</subviews>
57-
<constraints>
58-
<constraint firstItem="SdQ-xn-v14" firstAttribute="leading" secondItem="wbD-Qx-IG5" secondAttribute="trailing" id="NmY-BC-rSb"/>
59-
<constraint firstItem="wbD-Qx-IG5" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="topMargin" constant="5" id="Wz9-XC-sH4"/>
60-
<constraint firstAttribute="trailing" secondItem="SdQ-xn-v14" secondAttribute="trailing" id="Xtw-n7-vXM"/>
61-
<constraint firstItem="SdQ-xn-v14" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" id="ZFX-sQ-V5W"/>
62-
<constraint firstAttribute="bottomMargin" secondItem="wbD-Qx-IG5" secondAttribute="bottom" constant="5" id="c3q-Gf-seA"/>
63-
<constraint firstAttribute="bottom" secondItem="SdQ-xn-v14" secondAttribute="bottom" id="oyi-zQ-f4g"/>
64-
<constraint firstItem="wbD-Qx-IG5" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leadingMargin" id="qQd-GP-Nj1"/>
65-
</constraints>
6637
</tableViewCellContentView>
6738
<viewLayoutGuide key="safeArea" id="njF-e1-oar"/>
68-
<connections>
69-
<outlet property="bottomLabel" destination="hrx-8V-5Ga" id="90Q-2X-dsF"/>
70-
<outlet property="toggleSwitch" destination="eQm-bW-led" id="Muy-eR-8sm"/>
71-
<outlet property="topLabel" destination="B98-58-fIv" id="CHd-KO-qBg"/>
72-
</connections>
7339
<point key="canvasLocation" x="-5" y="-126.5"/>
7440
</tableViewCell>
7541
</objects>

0 commit comments

Comments
 (0)