Search code examples
iosswiftuitableviewswipe-gesture

Selecting 'Delete' in UITableView cell swipe, but 'commit editingStyle' not called


I have the following -

    class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource

With these methods -

    public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }

    public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
    {
        print("editingStyle")
    }

When I left swipe on a cell, the 'Delete' option shows as expected - Delete action shown

If I -

  1. tap on Delete, the method above with parameter 'commit editingStyle' is not called
  2. swipe left all the way to the left side of the view instead of stopping when Delete appears, the method is called.

It seems to me this method would be called with both #1 & #2.

Can someone help me understand what I've done wrong?

EDIT - With DonMag's input, solved issue with:

     override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let hitView = super.hitTest(point, with: event) {
            let isSwipeButton = String(describing: type(of: hitView)) ==  "UISwipeActionStandardButton"
            if hitView.isKind(of: BTTableCellContentView.self)  || isSwipeButton {
                return hitView
            }
        }
        return nil;
    }

Solution

  • It appears commit is not being called because that class is overriding hitTest:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let hitView = super.hitTest(point, with: event) , hitView.isKind(of: BTTableCellContentView.self) {
            return hitView
        }
        return nil;
    }
    

    and returning nil because hitView is then the action button.

    Simply removing it may/will cause issues (first one noticed is that the "drop down" doesn't close when tapping outside of it).

    So you'll need to edit that function... might take a little work, but that's the place to start.