自定义表格视图单元格中的TouchesBegan取消didselectrowatindexpath函数

问题描述 投票:0回答:1

我拥有带有自定义单元格的表格视图。在自定义单元格中,我有View,它是Custom UIView类。在此自定义UIView类中,我重写touchesBegan和touchesEnded方法以在按下视图时更改自定义视图的背景色。在表格单元格中,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的选定行。但是什么都没打印。

swift uitableview didselectrowatindexpath touchesbegan
1个回答
0
投票

在touchBegan()方法内使用超级方法。

覆盖func touchesBegan(_ touches:设置,事件:UIEvent?){self.backgroundColor = UIColor(红色:216/255,绿色:216/255,蓝色:216/255,alpha:0.3)super.touchesBegan(touches,with:event)}

© www.soinside.com 2019 - 2024. All rights reserved.