[英]Touches should cancel function for UITableView is not called
我有一個表格視圖,在每個 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)
}
}
但是,它不起作用。 每當我滾動表格視圖時,function 甚至都不會被調用。 我在這里想念什么? 非常感謝您的意見。 謝謝大家。
您需要創建一個UITableView
子類
class SubTbl:UITableView {
// add your method
}
然后將其分配給 IB 中的該表或在代碼中使用它
子類UITableView
根據Apple 文檔將 tableView canCancelContentTouches
設置為true
如果 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.