How can I get IndexPath of currently selected tableviewcell by voice over?

971 views Asked by At

I'm using Voice Over in my application. I'm having hard time to figure out which table cell is currently selected when voice over is on. How can I know whenever user initiates single tap or navigate through any tableviewcell?

1

There are 1 answers

5
Boris Dušek On

These are the things you can try:

  • use the UIAccessibilityFocusedElement global function
  • override accessibilityElementDidBecomeFocused and accessibilityElementDidLoseFocus on the cell
  • observe the UIAccessibilityElementFocused notification in NotificationCenter in situations where you need it (e.g. when the view controller for the table in question is showing)

Also what element will report focus will most probably depend on whether your UITableViewCell has isAccessibilityElement set to true or false.

While the above will probably help you with literally what you asked, it is also possible that your overall approach to accessibility in this situation might be wrong if you need the above information. If you share more info on the bigger picture / motivation what you are trying to achieve, it might turn out that the information about focused element might not be needed at all and that another solution is more proper.

If what you need is to add a hint for swiping, you can simply set accessibilityHint on the proper element (if you set isAccessibilityElement = true on the whole cell, then set that on the whole cell, otherwise try setting it on the label that VoiceOver reads in the cell), e.g. when you configure the cell for display (usually in tableView(_:cellForRowAt:)). In such case, you will not need to observe which element is focused, and simply let VoiceOver read hint available on that particular element/cell.