gpt4 book ai didi

swift - TouchesBegan在自定义tableview单元格中取消didselectrowatindexpath函数

转载 作者:行者123 更新时间:2023-11-30 10:39:52 25 4
gpt4 key购买 nike

我有带有自定义单元格的表格 View 。在自定义单元格中,我有 View,它是自定义 UIView 类。在此自定义 UIView 类中,我重写touchesBegan 和touchesEnded 方法,以在按下 View 时更改自定义 View 的背景颜色。在表格 View 单元格中touchesBegan和touchesEnded方法完美地工作。当我按下表格 View 单元格时, View 的背景正在改变。但在这种情况下,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) { 打印(索引路径.行) }

我希望打印 tableView 的选定行。但没有打印任何内容。

最佳答案

在 touchBegan() 方法中使用 super 方法。当一个类继承另一个类时,继承的类称为子类,它继承的类称为其父类(super class)。这里的父类(super class)是UIView。添加super.OverrideMethodName()后,它会覆盖UIViewtouchBegan()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundColor = UIColor(red: 216/255, green: 216/255, blue: 216/255,alpha: 0.3)
super.touchesBegan(touches, with: event)
}

关于swift - TouchesBegan在自定义tableview单元格中取消didselectrowatindexpath函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57065710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com