Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewRowAction vs UISwipeActionsConfiguration

Tags:

ios

swift

The two APIs seem to get the same result. In which case is it better to use one over the other?

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  let deleteAction = UITableViewRowAction(style: .destructive,
                                          title: "Delete") { (action, indexPath) in
    print("DELETE")
  }
  return [deleteAction]
}

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  let DeleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, success) in
      print("Delete")
  })
  DeleteAction.backgroundColor = .red
  return UISwipeActionsConfiguration(actions: [DeleteAction])
}
like image 654
Turnipdabeets Avatar asked Dec 02 '18 06:12

Turnipdabeets


2 Answers

It does basically the same, but swipe actions are available since iOS 11 was released and have some new features:

  • You are able to set actions for trailing swipe as well as for leading swipe
  • You can set image of action action.image = UIImage(...). If there is enough space it shows image as well as title enter image description here

Also, you should use swipe actions because they are preferred and in the future updates UITableViewRowActions will be deprecated how UITableView header comment can tell us:

Use -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: instead of this method, which will be deprecated in a future release.

like image 196
Robert Dresler Avatar answered Oct 15 '22 14:10

Robert Dresler


Here is the code which is working for me

internal func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

   // let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
    let contextItem = UIContextualAction(style: .destructive, title: "") {  (contextualAction, view, boolValue) in
    // delete item at indexPath
       //if self.isfilterenabled == true {
         //   return
        //}


        if self.isfilterenabled == true {
            //entryFilter.filteredEntries[indexPath.row]
            self.entryFilter.filteredEntries.remove(at: indexPath.row)
        } else {
            self.data.remove(at: indexPath.row)
        }

        self.table.deleteRows(at: [indexPath], with: .fade)
        self.save()
    }



    //let share = UITableViewRowAction(style: .normal, title: "SavePDF") { (action, indexPath) in
        // share item at indexPath
    let contextItemSave = UIContextualAction(style: .normal, title: "") {  (contextualAction, view, boolValue) in





        let alert  = UIAlertController(title: "Done! ", message: "PDF has been saved " ,preferredStyle: .alert)

        let okAction = UIAlertAction(title: "OK ", style: .default, handler: nil)
        alert.addAction(okAction)

        alert.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

        // exclude some activity types from the list (optional)
        //activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]

        // present the view controller
        self.present(alert, animated: true, completion: nil)
        // present(alert, animated : true, completion : nil )

    }

   // share.backgroundColor = UIColor.blue

    contextItemSave.image = UIImage(named:"PDF.jpg")
    contextItem.image = UIImage(named:"delete.jpg")
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem,contextItemSave])

    return swipeActions
    //return [delete, share]
}`
like image 1
Guri S Avatar answered Oct 15 '22 14:10

Guri S