Introduction
Swipe actions let users quickly do tasks by swiping on a list item. It makes apps easier and faster to use.
Swipe actions let users quickly do tasks by swiping on a list item. It makes apps easier and faster to use.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completionHandler) in
// handle delete
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [deleteAction])
}trailingSwipeActionsConfigurationForRowAt for swipe actions from right to left.UIContextualAction for each swipe button with style and title.func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { action, view, completion in
print("Delete tapped")
completion(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let mark = UIContextualAction(style: .normal, title: "Mark") { action, view, completion in
print("Mark tapped")
completion(true)
}
mark.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [mark])
}This app shows a list of fruits. Swipe left on any fruit to see a red Delete button. Tapping Delete prints which fruit was deleted.
import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() var items = ["Apple", "Banana", "Cherry"] override func viewDidLoad() { super.viewDidLoad() tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self view.addSubview(tableView) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = items[indexPath.row] return cell } func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let delete = UIContextualAction(style: .destructive, title: "Delete") { action, view, completion in print("Deleted: \(self.items[indexPath.row])") completion(true) } return UISwipeActionsConfiguration(actions: [delete]) } }
Swipe actions only work on UITableView or UICollectionView rows.
Always call completion(true) after handling the action to close the swipe menu.
You can add multiple actions by putting more UIContextualAction in the array.
Swipe actions let users quickly do tasks by swiping on list items.
Use UIContextualAction inside swipe configuration methods to add buttons.
Call completion handler to finish the swipe action.
UIContextualAction in a swipe action on iOS?UIContextualAction for a swipe action in Swift?let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in
print("Deleted")
completionHandler(true)
}
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configurationlet editAction = UIContextualAction(style: .normal, title: "Edit") { action, view, _ in
print("Edit tapped")
}Pass the test and this pattern moves unknown