Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableVIew swipe trailingSwipeActionsConfigurationForRowAt not working

New iOS 11 UITableView Swipe action not getting called. The delegate and datasource are working fine for the table.

I am not able to swipe and see the menu items.

Below is my code for the same.

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
        print("Leading Action style .normal")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .destructive, title: "Trailing & .destructive") { (contextualAction, view, boolValue) in
        print("Trailing Action style .destructive")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

I tried to call below and its working fine.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

    }
}

Any hint in right direction is highly appreciated.

like image 301
Ekra Avatar asked Oct 27 '25 10:10

Ekra


1 Answers

You need to pass true to UIContextualAction in the closure boolValue(true). Otherwise the handler won't allow the action.

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
        boolValue(true) // pass true if you want the handler to allow the action
        print("Leading Action style .normal")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}
like image 125
Au Ris Avatar answered Oct 28 '25 23:10

Au Ris



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!