【发布时间】:2019-11-25 16:32:09
【问题描述】:
我有自定义单元格的表格视图。在自定义单元格中,我有 View 这是自定义 UIView 类。在这个自定义 UIView 类中,我重写 touchesBegan 和 touchesEnded 方法以在按下视图时更改自定义视图的背景颜色。在 tableview 单元格中,touchesBegan 和 touchesEnded 方法工作得很好。当我按下 tableview 单元格时,视图的背景正在改变。但在这种情况下,tableView 的 didselectrowat 功能不起作用。此自定义 UiView 类取消了 didSelectRowat 函数。有什么解决办法吗?
自定义类如下:
类 BildirisItemBackground: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundColor = UIColor(red: 216/255, green: 216/255, blue: 216/255, alpha: 0.3)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05, execute: {
( self.backgroundColor = UIColor(red: 216/255, green: 216/255, blue: 216/255, alpha: 0.0))
})
}
}
还有didSelectrowat函数:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 打印(indexPath.row) }
我希望打印 tableView 的选定行。但是什么都没有打印出来。
【问题讨论】:
-
尝试调用super方法。极好的。 touchesBegan(....)
-
非常感谢它正在工作。
标签: swift uitableview didselectrowatindexpath touchesbegan