【问题标题】:Touches should cancel function for UITableView is not called未调用 UITableView 的触摸应取消功能
【发布时间】:2020-11-01 08:12:14
【问题描述】:

我有一个表格视图,它在每个 TableView 单元格中有一个 UIImage 和一些 UIButton 对象。当我滚动表格视图时,它总体上工作得很好。但是,如果我触摸其中一个 UIButton 项目来滚动表格视图,则 UIButton 似乎会窃取触摸并且表格视图不会滚动。相反,UIButton 项目似乎被选中。即使用户在开始滚动时触摸按钮,我也希望能够滚动表格视图。所以,我在这里搜索了解决方案,尝试了以下方法。

extension UITableView {

    override public func touchesShouldCancel(in view: UIView) -> Bool {

        print("the touchesShouldCancel function is called.")

        if view is UIButton {
            return true
        }

        return super.touchesShouldCancel(in: view)
    }

}

但是,它不起作用。每当我滚动表格视图时,甚至都不会调用该函数。我在这里想念什么?非常感谢您的意见。谢谢大家。

【问题讨论】:

    标签: ios swift uitableview uibutton touchescancelled


    【解决方案1】:

    子类UITableView 按照Apple docs 将tableView canCancelContentTouches 设置为true

    如果值为 canCancelContentTouches 属性为 false

    class YourTableView:UITableView {
    
        override func awakeFromNib() {
            canCancelContentTouches = true
            delaysContentTouches = false
        }
    
        override func touchesShouldCancel(in view: UIView) -> Bool {
            
        }
    }
    

    【讨论】:

    • 子类化似乎没有任何区别。我刚刚尝试了您的代码,但表格视图单元格中的按钮仍然窃取滚动触摸。不过还是谢谢。
    • 你设置canCancelContentTouches
    • 是的,因为您的代码包含它。此外,在我的 IB 中,检查了 canCancelContentTouches。
    • 还添加 delaysContentTouches = false 更新了我的答案
    • 哇!它现在按我的意愿工作。非常感谢。您能否告诉我,子类化与我最初所做的那样仅仅用扩展覆盖有什么区别?
    【解决方案2】:

    你需要创建一个UITableView 子类

    class SubTbl:UITableView {
       // add your method
    }
    

    然后将其分配给IB中的那个表或在代码中使用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多