Skip to content

Commit d7257a0

Browse files
author
Fernando Fernandes
committed
Update example for the Sixth Test
1 parent 4e269dc commit d7257a0

File tree

5 files changed

+87
-18
lines changed

5 files changed

+87
-18
lines changed

Xcode/PaintCodeTests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
071D1DC021FE217200A096F0 /* CircleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 071D1DBF21FE217200A096F0 /* CircleView.swift */; };
1313
071D1DC221FE218300A096F0 /* CircleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 071D1DC121FE218300A096F0 /* CircleViewController.swift */; };
1414
073DC50621FE290E009B6ECE /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 073DC50521FE290E009B6ECE /* README.md */; };
15+
077F2808222E6C4B0011CA5F /* OptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 077F2807222E6C4B0011CA5F /* OptionsView.swift */; };
1516
ED29A579222C44DB00493720 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED29A578222C44DB00493720 /* TableViewController.swift */; };
1617
ED29A57B222C474C00493720 /* CustomCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED29A57A222C474C00493720 /* CustomCell.swift */; };
1718
ED2E0C3F213DD8110029BC7D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED2E0C3E213DD8110029BC7D /* AppDelegate.swift */; };
@@ -32,6 +33,7 @@
3233
071D1DBF21FE217200A096F0 /* CircleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircleView.swift; sourceTree = "<group>"; };
3334
071D1DC121FE218300A096F0 /* CircleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircleViewController.swift; sourceTree = "<group>"; };
3435
073DC50521FE290E009B6ECE /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
36+
077F2807222E6C4B0011CA5F /* OptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptionsView.swift; sourceTree = "<group>"; };
3537
ED29A578222C44DB00493720 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
3638
ED29A57A222C474C00493720 /* CustomCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCell.swift; sourceTree = "<group>"; };
3739
ED2E0C3B213DD8110029BC7D /* PaintCodeTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PaintCodeTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -104,6 +106,7 @@
104106
071D1DBB21FE1C4400A096F0 /* StarView.swift */,
105107
ED2E0C5D213DD8F60029BC7D /* TextButton.swift */,
106108
ED2E0C5C213DD8F60029BC7D /* TreeView.swift */,
109+
077F2807222E6C4B0011CA5F /* OptionsView.swift */,
107110
);
108111
path = View;
109112
sourceTree = "<group>";
@@ -215,6 +218,7 @@
215218
ED2E0C67213DD9100029BC7D /* StyleKit.swift in Sources */,
216219
ED2E0C60213DD8F70029BC7D /* TextButton.swift in Sources */,
217220
ED29A579222C44DB00493720 /* TableViewController.swift in Sources */,
221+
077F2808222E6C4B0011CA5F /* OptionsView.swift in Sources */,
218222
071D1DC221FE218300A096F0 /* CircleViewController.swift in Sources */,
219223
ED2E0C5F213DD8F70029BC7D /* TreeView.swift in Sources */,
220224
ED2E0C59213DD8E20029BC7D /* ClockHandViewController.swift in Sources */,

Xcode/PaintCodeTests/Controller/TableViewController.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@ class TableViewController: UITableViewController {
2424
var cell = UITableViewCell()
2525

2626
if let customCell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomCell {
27-
cell = customCell
28-
29-
// Icon from PaintCode. In this example I'll be using two different icons, that's why I first create this
30-
// placeholder `UIView`.
31-
var icon = UIView(frame: customCell.iconView.frame)
32-
27+
28+
// Logic around which option to display
3329
if indexPath.row < 20 {
34-
icon = StarView(frame: icon.frame)
35-
customCell.label.text = "Star"
36-
30+
customCell.option = .star
3731
} else {
38-
icon = TreeView(frame: icon.frame)
39-
customCell.label.text = "Tree"
32+
customCell.option = .tree
4033
}
41-
42-
customCell.iconView.addSubview(icon)
34+
35+
// Update the cell to be returned.
36+
cell = customCell
4337
}
4438

4539
return cell

Xcode/PaintCodeTests/Storyboard/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@
185185
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
186186
<autoresizingMask key="autoresizingMask"/>
187187
<subviews>
188-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="x8Z-o1-aJn">
188+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="x8Z-o1-aJn" customClass="OptionsView" customModule="PaintCodeTests" customModuleProvider="target">
189189
<rect key="frame" x="10" y="2" width="40" height="40"/>
190-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
190+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
191191
<constraints>
192192
<constraint firstAttribute="width" constant="40" id="2T4-tw-W5R"/>
193193
<constraint firstAttribute="height" constant="40" id="6P5-57-gJC"/>

Xcode/PaintCodeTests/View/CustomCell.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,33 @@
99
import UIKit
1010

1111
class CustomCell: UITableViewCell {
12+
13+
// MARK: - Public Properties
14+
15+
// Option that should currently be displayed. Default is .star (for no particular reason).
16+
var option: OptionsView.Option = .star {
17+
didSet {
18+
iconView.currentOption = option
19+
updateLabelText()
20+
}
21+
}
1222

13-
// MARK: - Properties
23+
// MARK: - Private Properties
1424

15-
@IBOutlet weak var iconView: UIView!
16-
@IBOutlet weak var label: UILabel!
25+
@IBOutlet private weak var iconView: OptionsView!
26+
@IBOutlet private weak var label: UILabel!
27+
}
28+
29+
// MARK: - Private
30+
31+
private extension CustomCell {
32+
33+
func updateLabelText() {
34+
switch option {
35+
case .star:
36+
label.text = "Star"
37+
case .tree:
38+
label.text = "Tree"
39+
}
40+
}
1741
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// OptionsView.swift
3+
// PaintCodeTests
4+
//
5+
// Created by Fernando Fernandes on 05.03.19.
6+
// Copyright © 2019 backslash-f. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@IBDesignable class OptionsView: UIView {
12+
13+
// MARK: - Properties
14+
15+
// Allowed options.
16+
enum Option {
17+
case star, tree
18+
}
19+
20+
// Option that should currently be displayed. Default is .star (for no particular reason).
21+
var currentOption: Option = .star {
22+
didSet {
23+
setNeedsDisplay() // Force redrawing.
24+
}
25+
}
26+
27+
// MARK: - Lifecycle
28+
29+
override func draw(_ rect: CGRect) {
30+
drawIcon(rect)
31+
}
32+
}
33+
34+
// MARK: - Private
35+
36+
private extension OptionsView {
37+
38+
/// Logic to decide which icon to display.
39+
func drawIcon(_ rect: CGRect) {
40+
switch currentOption {
41+
case .star:
42+
StyleKit.drawStarIcon(frame: rect)
43+
case .tree:
44+
StyleKit.drawTreeIcon(frame: rect)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)