简体   繁体   English

触摸应该取消 function 因为 UITableView 没有被调用

[英]Touches should cancel function for UITableView is not called

I have a table view that has a UIImage and some UIButton objects in each TableView cell.我有一个表格视图,在每个 TableView 单元格中有一个 UIImage 和一些 UIButton 对象。 When I scroll the table view, it works quite well overall.当我滚动表格视图时,它总体上工作得很好。 However, if I touch one of the UIButton items to scroll the table view, the UIButton seems to steal the touches and the table view does not scroll.但是,如果我触摸 UIButton 项目之一来滚动表格视图,则 UIButton 似乎窃取了触摸并且表格视图不会滚动。 Instead the UIButton items appears to be selected instead.相反,似乎选择了 UIButton 项目。 I would like to be able to scroll the table view even when the user touches buttons when starting to scroll.即使用户在开始滚动时触摸按钮,我也希望能够滚动表格视图。 So, I searched for solutions here, tried the following.所以,我在这里搜索了解决方案,尝试了以下方法。

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

}

However, it doesn't work.但是,它不起作用。 The function does not even get called whenever I scroll the table view.每当我滚动表格视图时,function 甚至都不会被调用。 What am I missing here?我在这里想念什么? I would greatly appreciate your input.非常感谢您的意见。 Thanks all.谢谢大家。

You need to make a UITableView subclass您需要创建一个UITableView子类

class SubTbl:UITableView {
   // add your method
}

Then assign it to that table in IB or use it in code然后将其分配给 IB 中的该表或在代码中使用它

Subclass UITableView Set tableView canCancelContentTouches to true as per Apple docs子类UITableView根据Apple 文档将 tableView canCancelContentTouches设置为true

The scroll view does not call this method if the value of the canCancelContentTouches property is false如果 canCancelContentTouches 属性的值为 false,则滚动视图不会调用此方法

class YourTableView:UITableView {

    override func awakeFromNib() {
        canCancelContentTouches = true
        delaysContentTouches = false
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM