jh95
jh95

Reputation: 399

UITapGestureRecognizer won't work for custom UIView class

I have a custom UIView class which creates a checkbox. This UIView is in a custom table view cell.

I have this code in cellForRowAt

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TransactionsTableViewCell
    cell.isUserInteractionEnabled = true
    cell.addSubview(cell.test)
    let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.recurringChange(_:)))
    cell.test.isUserInteractionEnabled = true
    cell.test.addGestureRecognizer(gesture)
  }

I have have this function in the ViewController class

@objc func recurringChange(_ sender: 
UITapGestureRecognizer) {
    print("test")
}

When test view is tapped, it does not print test. I tried this with a normal UIView (not custom), and it worked exactly as expected.

If this helps, here is a link to the custom class: https://github.com/vladislav-k/VKCheckbox

Upvotes: 0

Views: 331

Answers (1)

E.Coms
E.Coms

Reputation: 11531

I have tested the code in Xcode 10 and there is no problem.

You may check " cell.isUserInteractionEnabled = true" and "cell.addSubview(test)" is added to cell before you add TapGesture.

Upvotes: 1

Related Questions