Skip to content

Commit f246b07

Browse files
dmonagleSplittyDev
andauthored
Add an introspect method for UITableViewCell (#18)
* Adds an introspect method for UITableViewCell * Added NSTableCellView introspection Added line to CHANGELOG Added tests for UIKit and AppKit * Removed additional spaces Co-authored-by: SplittyDev <splittydev@gmail.com>
1 parent 868a7e8 commit f246b07

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changelog
33

44
## master
55

6+
- Added `introspectTableViewCell`
67
- Add Github Action
78
- Added `.introspectTextView()`.
89
- Update CircleCI config to use Xcode 12.4.0

Introspect/ViewExtensions.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ extension View {
7575
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
7676
}
7777

78+
/// Finds a `UITableViewCell` from a `SwiftUI.List`, or `SwiftUI.List` child. You can attach this directly to the element inside the list.
79+
public func introspectTableViewCell(customize: @escaping (UITableViewCell) -> ()) -> some View {
80+
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
81+
}
82+
7883
/// Finds a `UIScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
7984
public func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
8085
if #available(iOS 14.0, tvOS 14.0, macOS 11.0, *) {
@@ -144,7 +149,12 @@ extension View {
144149
public func introspectTableView(customize: @escaping (NSTableView) -> ()) -> some View {
145150
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
146151
}
147-
152+
153+
/// Finds a `NSTableCellView` from a `SwiftUI.List`, or `SwiftUI.List` child. You can attach this directly to the element inside the list.
154+
public func introspectTableViewCell(customize: @escaping (NSTableCellView) -> ()) -> some View {
155+
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
156+
}
157+
148158
/// Finds a `NSScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
149159
public func introspectScrollView(customize: @escaping (NSScrollView) -> ()) -> some View {
150160
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)

IntrospectTests/AppKitTests.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@ private struct ListTestView: View {
2828

2929
let spy1: () -> Void
3030
let spy2: () -> Void
31-
31+
let spyCell1: () -> Void
32+
let spyCell2: () -> Void
33+
3234
var body: some View {
3335
List {
3436
Text("Item 1")
3537
Text("Item 2")
3638
.introspectTableView { tableView in
3739
self.spy2()
3840
}
41+
.introspectTableViewCell { cell in
42+
self.spyCell2()
43+
}
44+
3945
}
4046
.introspectTableView { tableView in
4147
self.spy1()
4248
}
49+
.introspectTableViewCell { cell in
50+
self.spyCell1()
51+
}
4352
}
4453
}
4554

@@ -151,17 +160,21 @@ private struct SegmentedControlTestView: View {
151160
class AppKitTests: XCTestCase {
152161

153162
func testList() {
154-
155163
let expectation1 = XCTestExpectation()
156164
let expectation2 = XCTestExpectation()
165+
let cellExpectation1 = XCTestExpectation()
166+
let cellExpectation2 = XCTestExpectation()
167+
157168
let view = ListTestView(
158169
spy1: { expectation1.fulfill() },
159-
spy2: { expectation2.fulfill() }
170+
spy2: { expectation2.fulfill() },
171+
spyCell1: { cellExpectation1.fulfill() },
172+
spyCell2: { cellExpectation2.fulfill() }
160173
)
161174
TestUtils.present(view: view)
162-
wait(for: [expectation1, expectation2], timeout: TestUtils.Constants.timeout)
175+
wait(for: [expectation1, expectation2, cellExpectation1, cellExpectation2], timeout: TestUtils.Constants.timeout)
163176
}
164-
177+
165178
func testScrollView() {
166179

167180
let expectation1 = XCTestExpectation()

IntrospectTests/UIKitTests.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,27 @@ private struct ListTestView: View {
112112

113113
let spy1: () -> Void
114114
let spy2: () -> Void
115-
115+
let spyCell1: () -> Void
116+
let spyCell2: () -> Void
117+
116118
var body: some View {
117119
List {
118120
Text("Item 1")
119121
Text("Item 2")
120122
.introspectTableView { tableView in
121123
self.spy2()
122124
}
125+
.introspectTableViewCell { cell in
126+
self.spyCell2()
127+
}
128+
123129
}
124130
.introspectTableView { tableView in
125131
self.spy1()
126132
}
133+
.introspectTableViewCell { cell in
134+
self.spyCell1()
135+
}
127136
}
128137
}
129138

@@ -294,12 +303,17 @@ class UIKitTests: XCTestCase {
294303

295304
let expectation1 = XCTestExpectation()
296305
let expectation2 = XCTestExpectation()
306+
let cellExpectation1 = XCTestExpectation()
307+
let cellExpectation2 = XCTestExpectation()
308+
297309
let view = ListTestView(
298310
spy1: { expectation1.fulfill() },
299-
spy2: { expectation2.fulfill() }
311+
spy2: { expectation2.fulfill() },
312+
spyCell1: { cellExpectation1.fulfill() },
313+
spyCell2: { cellExpectation2.fulfill() }
300314
)
301315
TestUtils.present(view: view)
302-
wait(for: [expectation1, expectation2], timeout: TestUtils.Constants.timeout)
316+
wait(for: [expectation1, expectation2, cellExpectation1, cellExpectation2], timeout: TestUtils.Constants.timeout)
303317
}
304318

305319
func testScrollView() {

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ List {
8484
.introspectTableView { tableView in
8585
tableView.separatorStyle = .none
8686
}
87+
.introspectTableViewCell { cell in
88+
let backgroundView = UIView()
89+
backgroundView.backgroundColor = .clear
90+
cell.selectedBackgroundView = backgroundView
91+
}
8792
```
8893

8994
### ScrollView

0 commit comments

Comments
 (0)