Adding swipe left on tableView cell with custom background color
In tableView, we can add swipe left or right to let user perform an action based on selected cell.
first we need to setup tableView delegate to the parent viewController then set this 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])
}
Linked Notes:
Today I learn
I believe that we can learn something new everyday, that’s why I set this page to record what I learn during the day.