1
votes

I have tableview with custom cells. In custom cell i have View which is Custom UIView class. In this custom UIView class i override touchesBegan and touchesEnded methods to change background color of the custom view when the view is pressed. In tableview cell touchesBegan and touchesEnded methods works perfectly. Background of the view is changing when i pressed tableview cell. But in this case didselectrowat function of tableView is not working. This Custom UiView class cancels didSelectRowat function. Is there any solution of the problem?

Custom class is below:

class 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))
    })
}

}

And didSelectrowat function:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print(indexPath.row) }

I expect that printing selected row of tableView. But nothing is printing.

1
Try to call the super method. super. touchesBegan(....)A. Amini
Thank you very much it is working.Nicat Güliyev

1 Answers

1
votes

Use super method inside touchBegan() method. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Here super class is UIView. After adding super.OverrideMethodName(), it is overriding UIView's touchBegan().

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)
}