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])
}
It does basically the same, but swipe actions are available since iOS 11 was released and have some new features:
action.image = UIImage(...)
. If there is enough space it shows image as well as title
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.
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]
}`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With