Touches should cancel 函数,因为 UITableView 没有被调用
Touches should cancel function for UITableView is not called
我有一个 table 视图,每个 TableView 单元格中都有一个 UIImage 和一些 UIButton 对象。当我滚动 table 视图时,它总体上运行良好。但是,如果我触摸其中一个 UIButton 项目来滚动 table 视图,UIButton 似乎窃取了触摸并且 table 视图不滚动。相反,似乎选择了 UIButton 项目。即使用户在开始滚动时触摸按钮,我也希望能够滚动 table 视图。所以,我在这里搜索解决方案,尝试了以下方法。
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)
}
}
然而,它不起作用。每当我滚动 table 视图时,该函数甚至都不会被调用。我在这里错过了什么?非常感谢您的意见。谢谢大家
您需要创建一个 UITableView
子类
class SubTbl:UITableView {
// add your method
}
然后在IB中赋给那个table或者在代码中使用
子类 UITableView
根据 Apple docs
将 tableView canCancelContentTouches
设置为 true
The scroll view does not call this method if the value of the
canCancelContentTouches property is false
class YourTableView:UITableView {
override func awakeFromNib() {
canCancelContentTouches = true
delaysContentTouches = false
}
override func touchesShouldCancel(in view: UIView) -> Bool {
}
}
我有一个 table 视图,每个 TableView 单元格中都有一个 UIImage 和一些 UIButton 对象。当我滚动 table 视图时,它总体上运行良好。但是,如果我触摸其中一个 UIButton 项目来滚动 table 视图,UIButton 似乎窃取了触摸并且 table 视图不滚动。相反,似乎选择了 UIButton 项目。即使用户在开始滚动时触摸按钮,我也希望能够滚动 table 视图。所以,我在这里搜索解决方案,尝试了以下方法。
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)
}
}
然而,它不起作用。每当我滚动 table 视图时,该函数甚至都不会被调用。我在这里错过了什么?非常感谢您的意见。谢谢大家
您需要创建一个 UITableView
子类
class SubTbl:UITableView {
// add your method
}
然后在IB中赋给那个table或者在代码中使用
子类 UITableView
根据 Apple docs
canCancelContentTouches
设置为 true
The scroll view does not call this method if the value of the canCancelContentTouches property is false
class YourTableView:UITableView {
override func awakeFromNib() {
canCancelContentTouches = true
delaysContentTouches = false
}
override func touchesShouldCancel(in view: UIView) -> Bool {
}
}