@@ -97,11 +97,11 @@ public class UIKeyCommandTableView: UITableView {
9797 var keyCommands = [ UIKeyCommand] ( )
9898
9999 selectPreviousKeyCommandOptions. forEach {
100- keyCommands. append ( UIKeyCommand ( $0, action: #selector( selectPrevious ) ) )
100+ keyCommands. append ( UIKeyCommand ( $0, action: #selector( selectPreviousRow ) ) )
101101 }
102102
103103 selectNextKeyCommandOptions. forEach {
104- keyCommands. append ( UIKeyCommand ( $0, action: #selector( selectNext ) ) )
104+ keyCommands. append ( UIKeyCommand ( $0, action: #selector( selectNextRow ) ) )
105105 }
106106
107107 activateSelectionKeyCommandOptions. forEach {
@@ -147,6 +147,34 @@ public class UIKeyCommandTableView: UITableView {
147147 }
148148 }
149149
150+ @objc
151+ public func selectPreviousRow( ) {
152+ guard let currentSelection = indexPathForSelectedRow else {
153+ return selectRowIfPossible ( at: {
154+ if let lastVisibleRow = indexPathsForVisibleRows? . last {
155+ return lastVisibleRow
156+ }
157+ return indexPathForLastRowInLastSection
158+ } ( ) )
159+ }
160+
161+ selectRowIfPossible ( at: currentSelection. previousRow ( ) )
162+ }
163+
164+ @objc
165+ public func selectNextRow( ) {
166+ guard let currentSelection = indexPathForSelectedRow else {
167+ return selectRowIfPossible ( at: {
168+ for indexPath in indexPathsForVisibleRows ?? [ ] where isRowVisible ( at: indexPath) == . fullyVisible {
169+ return indexPath
170+ }
171+ return IndexPath ( row: NSNotFound, section: . zero)
172+ } ( ) )
173+ }
174+
175+ selectRowIfPossible ( at: currentSelection. nextRow ( ) )
176+ }
177+
150178}
151179
152180// MARK: - Selection
@@ -291,30 +319,18 @@ private extension UIKeyCommandTableView {
291319 delegate? . tableView ? ( self , accessoryButtonTappedForRowWith: selectedIndexPath)
292320 }
293321
294- func selectPrevious( ) {
295- guard let currentSelection = indexPathForSelectedRow else {
296- return selectRowIfPossible ( at: {
297- if let lastVisibleRow = indexPathsForVisibleRows? . last {
298- return lastVisibleRow
299- }
300- return indexPathForLastRowInLastSection
301- } ( ) )
322+ var indexPathForFirstVisibleRow : IndexPath ? {
323+ for indexPath in indexPathsForVisibleRows ?? [ ] where isRowVisible ( at: indexPath) == . fullyVisible {
324+ return indexPath
302325 }
303-
304- selectRowIfPossible ( at: currentSelection. previousRow ( ) )
326+ return . none
305327 }
306-
307- func selectNext( ) {
308- guard let currentSelection = indexPathForSelectedRow else {
309- return selectRowIfPossible ( at: {
310- if let firstVisibleRow = indexPathsForVisibleRows? . first {
311- return firstVisibleRow
312- }
313- return IndexPath ( row: NSNotFound, section: . zero)
314- } ( ) )
328+
329+ var indexPathForLastVisibleRow : IndexPath ? {
330+ for indexPath in ( indexPathsForVisibleRows ?? [ ] ) . reversed ( ) where isRowVisible ( at: indexPath) == . fullyVisible {
331+ return indexPath
315332 }
316-
317- selectRowIfPossible ( at: currentSelection. nextRow ( ) )
333+ return . none
318334 }
319335
320336 var selectableIndexPath : IndexPath ? {
@@ -338,7 +354,7 @@ private extension UIKeyCommandTableView {
338354private extension UIKeyCommandTableView {
339355
340356 /// Whether a row is fully visible, or if not if it’s above or below the viewport.
341- enum RowVisibility {
357+ enum RowVisibility : Hashable {
342358 case fullyVisible
343359 case notFullyVisible( ScrollPosition )
344360 }
0 commit comments