Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Customize UITableViewRowAction

I'm working with a tableView where the cells have insets and rounded corners. I want to apply the same style to the row actions. Since the customising options are kind of limited (background, style and title), is there a way to do that?

enter image description here

That's my code for the actions:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let removeSignal = UITableViewRowAction(style: .destructive, title: translate(.delete)) { (_, indexPath) in
        self.removeSignal(atIndex: indexPath.row)
    }        
    return [removeSignal]
}
like image 714
gmoraleda Avatar asked May 19 '26 03:05

gmoraleda


1 Answers

If you have iOS 11, you can use trailingSwipeActionsConfigurationForRowAt delegate to customise your row action:

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
            print("index path of delete: \(indexPath)")
            completionHandler(true)
        }
        let swipeConfig = UISwipeActionsConfiguration(actions: [deleteAction])
        return swipeConfig
    }

You can set your customised text in place of "Delete" inside UIContextualAction(style: .destructive, title: "Delete")

You can customise more with these properties of UIContextualAction:

deleteAction.backgroundColor = UIColor.red
deleteAction.image = UIImage(named: "delete")
like image 56
Arnab Avatar answered May 21 '26 16:05

Arnab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!