In UITableView, we can add swipe left or right to let user perform an action based on selected cell. We can also set custom background color to differentiate different action.
First we need to setup tableView delegate to the parent viewController then call this delegate function:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let amendAction = UIContextualAction(
style: .normal,
title: "Save",
handler: { [weak self] _, _, _ in
self?.handleAmendAction()
}
)
amendAction.backgroundColor = UIColor.green
let withdrawAction = UIContextualAction(
style: .normal,
title: "Delete",
handler: { [weak self] _, _, _ in
self?.handleWithdrawAction()
}
)
withdrawAction.backgroundColor = UIColor.delete
return UISwipeActionsConfiguration(actions: [withdrawAction, amendAction])
}