【问题标题】:TouchesBegan in custom tableview cell cancels didselectrowatindexpath function自定义表格视图单元格中的 TouchesBegan 取消了 didselectrowatindexpath 功能
【发布时间】: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


【解决方案1】:

在 touchBegan() 方法中使用 super 方法。当一个类从另一个类继承时,继承类称为子类,而它继承自的类称为其超类。这里的超类是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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多