Skip to content

Commit 7a1bd3d

Browse files
committed
Implement tableView(_:accessoryButtonTappedForRowWith:)
1 parent 44ecf42 commit 7a1bd3d

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

Source/Protocol/RowCompatible.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import Foundation
2828

2929
/// This protocol defines the compatible interface of a `NavigationRow` regardless of its associated cell type.
30-
public protocol NavigationRowCompatible: Row, RowStyle {}
30+
public protocol NavigationRowCompatible: Row, RowStyle {
31+
#if os(iOS)
32+
/// A closure that will be invoked when the accessory button is selected.
33+
var accessoryButtonAction: ((Row) -> Void)? { get }
34+
#endif
35+
}
3136

3237

3338
/// This protocol defines the compatible interface of a `TapActionRow` regardless of its associated cell type.

Source/QuickTableViewController.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ open class QuickTableViewController: UIViewController, UITableViewDataSource, UI
165165
}
166166
}
167167

168+
#if os(iOS)
169+
public func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
170+
switch tableContents[indexPath.section].rows[indexPath.row] {
171+
case let row as NavigationRowCompatible:
172+
row.accessoryButtonAction?(row)
173+
default:
174+
break
175+
}
176+
}
177+
#endif
178+
168179
}
169180

170181

Tests/ViewController/QuickTableViewDelegateSpec.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ internal final class QuickTableViewDelegateSpec: QuickSpec {
264264
}
265265
}
266266
}
267+
268+
#if os(iOS)
269+
270+
// MARK: - tableView(_:accessoryButtonTappedForRowWith:)
271+
describe("tableView(_:accessoryButtonTappedForRowWith:)") {
272+
let controller = QuickTableViewController()
273+
_ = controller.view
274+
var selectedIndex = -1
275+
276+
controller.tableContents = [
277+
Section(title: "NavigationRow", rows: [
278+
NavigationRow(text: "", detailText: .none, accessoryButtonAction: { _ in selectedIndex = 0 }),
279+
CustomNavigationRow(text: "", detailText: .none, accessoryButtonAction: { _ in selectedIndex = 1 }),
280+
NavigationRow<CustomCell>(text: "", detailText: .none, accessoryButtonAction: { _ in selectedIndex = 2 }),
281+
CustomNavigationRow<CustomCell>(text: "", detailText: .none, accessoryButtonAction: { _ in selectedIndex = 3 })
282+
])
283+
]
284+
285+
for index in 0...3 {
286+
it("should invoke action when accessory button at \(index) is selected") {
287+
controller.tableView(controller.tableView, accessoryButtonTappedForRowWith: IndexPath(row: index, section: 0))
288+
expect(selectedIndex).toEventually(equal(index))
289+
}
290+
}
291+
}
292+
293+
#endif
294+
267295
}
268296
}
269297

0 commit comments

Comments
 (0)