首页 > 解决方案 > 在 UITableView 单元格滑动中选择“删除”,但未调用“提交编辑样式”

问题描述

我有以下 -

    class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource

用这些方法——

    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")
    }

当我在单元格上左滑时,“删除”选项按预期显示 - 显示 删除操作

如果我 -

  1. 点击删除,上面带有参数'commit editingStyle'的方法不会被调用
  2. 一直向左滑动到视图的左侧,而不是在出现 Delete 时停止,调用该方法。

在我看来,此方法将同时使用#1 和#2 调用。

有人可以帮我理解我做错了什么吗?

编辑 - 通过 DonMag 的输入,解决了以下问题:

     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;
    }

标签: iosswiftuitableviewswipe-gesture

解决方案


似乎commit没有被调用,因为该类是重写的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;
}

并返回nil,因为hitView然后是操作按钮。

简单地删除它可能/会导致问题(第一个注意到的是“下拉”在点击它外部时不会关闭)。

所以你需要编辑那个函数......可能需要做一些工作,但这是开始的地方。


推荐阅读