Created button in table view when swiped but need it to perform action when tapped.

After creating a button in trailingSwipeActionsConfigurationForRowAt indexPath, how do I get the button to perform an action? For instance, if I swipe the row, the buttons created show but I want the row swiped to add a line throw the cell and to add the cell to a different table view.

Replies

What have you done till now? Please show your code.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let taken = UIContextualAction(style: .normal, title: "Taken") { (action, view, nil) in
        }
        taken.backgroundColor =  colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
        
        let added = UIContextualAction(style: .normal, title: "Added") { (action, view, nil) in
        }
        added.backgroundColor =  colorLiteral(red: 0.2436070212, green: 0.5393256153, blue: 0.1766586084, alpha: 1)
        
        let config = UISwipeActionsConfiguration(actions: [taken, added])
        config.performsFirstActionWithFullSwipe = false
        
        return config
    }

When I swipe, the buttons show but I want to get the buttons to do something when I tap the button.

Define it in the closure, just as


        let taken = UIContextualAction(style: .normal, title: "Taken") { (action, view, nil) in
            print("Just Swiped Taken")
        }



So, could you explain clearly what you want:

I want the row swiped to add a line throw the cell and to add the cell to a different table view.


the row swiped to add a line : add a line to what ?


throw the cell: do you mean throw the cell away ? Not clear

If you want to remove the cell :


do it in the main thread

DispatchQueue.main.async {
     tableView.deleteRows(at: [indexPath], with: .fade)
     // remove also from the dataSource array
}


add the cell to a different table view: So, at the end, the cell will move from table1 to table 2 ?

To add a cell to the other tableView (I assume you have a var to reference it tableView2 and you know the position where to insert (indexPath2)


DispatchQueue.main.async {
     tableView.deleteRows(at: [indexPath], with: .fade)
     self.tableView2.insertRows(at: [indexPath2], with : .bottom)
     // insert also in the dataSource2 array

}


If tableView2 is in another viewController, you could, instead of calling self.tableView2.insertRows, post a notification to get this VC to update its table.

If you want to do something when the button is tapped, you should not leave the hander of the `UIContextualAction` empty.


You may need to write something like this:

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let taken = UIContextualAction(style: .normal, title: "Taken") {
            action, sourceView, completionHandler in
            let actionPerformed = self.doTaken(action: action, sourceView: sourceView)
            completionHandler(actionPerformed)
        }
        taken.backgroundColor =  UIColor(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
          
        let added = UIContextualAction(style: .normal, title: "Added") {
            action, sourceView, completionHandler in
            let actionPerformed = self.doAdded(action: action, sourceView: sourceView)
            completionHandler(actionPerformed)
        }
        added.backgroundColor =  UIColor(red: 0.2436070212, green: 0.5393256153, blue: 0.1766586084, alpha: 1)
          
        let config = UISwipeActionsConfiguration(actions: [taken, added])
        config.performsFirstActionWithFullSwipe = false
          
        return config
    }

    func doTaken(action: UIContextualAction, sourceView: UIView) -> Bool {
        //Do somethiing for "Taken" button.
        //...
        return true //You may need to return false if the action is cancelled
    }
    
    func doAdded(action: UIContextualAction, sourceView: UIView) -> Bool {
        //Do somethiing for "Added" button.
        //...
        return true //You may need to return false if the action is cancelled
    }