简体   繁体   中英

How to make a UITextView detect the click on table cell

I am working on a project and one of the table views the text for it is a UITextView, but on the other one is a UILabel. The UILabel detects the click from the user as a click on the table cell, but the UITextView doesn't. Why is this happening? Is there any way to fix it?

Ensure the isSelectable property of your textView is true.

I figure it out, the cell view and text view are both scrow views and always will have conflict, so I need to uncheck the UserInteractions and Multiple Touch of the text view, booth set to false

Try that

yourTextView.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
@objc func myTargetFunction(textField: UITextView) {
      print("myTargetFunction")

}

I'm not sure from your question whether you're talking about selection on the UITableView row or the UITextView / UILabel itself.

If you're trying to set up a gesture recognizer on the UILabel or UITextView , I'm wondering if that's necessary or if you could just use didSelectRowAtIndexPath in your view controller.

eg

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    yourMethodHere(for: cell)
  }

Then you'd define in yourMethodHere() what behavior you want to happen when the cell is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM