Search code examples
iosswiftuitableviewswipe

In iOS, how can I detect if a cell in UITableview is in the process of being swiped?


I'm using leadingSwipeActionsConfigurationForRowAt to detect and process swipes. However, I need to be able to detect when the user started to swipe, but before it completes. The problem is that if I update the table using reloadData() or beginUpdates() / endUpdates() while a cell is being swiped, it resets the cell to the un-swiped position (and I don't like that kind of user experience). I want to be able to delay the cell/data update until the swipe is finished or cancelled.


Solution

  • Even though Andreas' answer is absolutely correct, I found an easier way to do it. Apparently, whenever leadingSwipeActionsConfigurationForRowAt is used, the cell is marked as being edited whenever it starts being swiped. And that allows us to use willBeginEditingRowAt and didEndEditingRowAt on the UITableView, without having to worry about the UITableViewCell

    var swipedRow: IndexPath? = nil
            
    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        swipedRow = indexPath
    }
        
    func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
        swipedRow = nil
    }
    

    And then you can just check if swipedRow == nil (plus, if it's not nil, you will know the indexPath of the row being swiped)