5

I'm creating an iOS App using Swift 4 and I'm not using Storyboards. To delete a row from the Table View Controller the user swipe left the row and then click the Delete Button.

Here is the code I'm using to implement that (no external libraries have been used):

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    self.isAccessibilityElement = true
    self.accessibilityLabel = "Delete row"


    let deleteAction = UIContextualAction(style: .normal , title: "DELETE") { (action, view, handler) in

        self.removeRowFromMyList(indexPath: indexPath.row)

        MyListController.stations.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .automatic)

        self.tableView.setEditing(false, animated: true)
        self.tableView.reloadData()
    }
    let swipeAction = UISwipeActionsConfiguration(actions: [deleteAction])
    swipeAction.performsFirstActionWithFullSwipe = false

    return swipeAction
}

I did check other questions and none of them address that. Please don't hesitate to comment here for any other information you need to know to solve this issue. Thanks :)

Pavlos
  • 906
  • 1
  • 11
  • 29

3 Answers3

2

Using Accessibility Custom Action from UIAccessibility by Apple

You just need to set Accessibility Custom Action:

cell.accessibilityCustomActions = [UIAccessibilityCustomAction(name: "Delete", target: self, selector: #selector(theCustomAction))]


@objc private func theCustomAction() -> Bool {
    //Do anything you want here
    return true
}

Update:

So I did recreate the project but this time I was using Storyboards (I wasn't the last time) and I imported from Cocoapods the SwipeCellKit Library and I followed their documentation and VoiceOver was working perfectly fine with deleting a cell from them indexPath.row with no problem.

Pavlos
  • 906
  • 1
  • 11
  • 29
2
  1. When Voice Over is turned on and the UITableViewCell is in focus, Voice Over would announce "Swipe Up or Down to Select a Custom Action, then double tap to activate"

  2. If the user follows the above mentioned instruction, the user would be able to select one of the many actions available and double tap to activate it

  3. After performing the action, Voice Over would announce "Performed action "

Note:

  • Advantage with using standard controls is that accessibility is mostly handled for you.
  • You don't have to worry about it breaking with newer versions of iOS
  • If system provides built in functionality then go with it.
user1046037
  • 16,755
  • 12
  • 92
  • 138
0

Implementing members of the UIAccessibilityCustomAction class will allow you to add additional functionality to the UITableViewCell. In cellForRowAt: IndexPath, add the follow code to attach a custom action to the cell.

cell.isAccessibilityElement = true
let customAction = UIAccessibilityCustomAction(name: "Delete Row", target: self, selector: #selector(deleteRowAction))
cell.accessibilityCustomActions = [selectAction, disclosureAction]

Selectors associated with the custom actions are powerful, but it is difficult to pass in parameters as arguments that indicate the cell or the index path. Furthermore, a tableView's didSelectRowAt: IndexPath isn't activated by the accessibility focus.

The solution here is to find the location of the VoiceOver accessibility focus and obtain information from the cell. This code can be included in your selector as shown below.

@objc func deleteRowAction() -> Bool {

        let focusedCell = UIAccessibilityFocusedElement(UIAccessibilityNotificationVoiceOverIdentifier) as! UITableViewCell

        if let indexPath = tableView?.indexPath(for: focusedCell) {
            // perform the custom action here using the indexPath information
        }

        return true
}
Brian Li
  • 2,260
  • 1
  • 15
  • 18
  • IMHO you don't need to write any accessibility specific code for swipe actions, just try voice over on default mail app, it would tell you actions available. You can just follow standard behaviour. Probably if you have custom actions then you could implement your suggestion – user1046037 Jun 15 '18 at 03:46
  • 1
    I did try that and didn't work for me, Maybe it was a bug or I was doing something wrong – Pavlos Jun 18 '18 at 09:30