IOS: select via UITextView on a custom UITableViewCell

I have a custom UITableViewCell with an image and a UITextView property. The text image extends to the edge of the cell. My problem is that the textview is not logged in the didSelectRowAtIndexPath file.

How can I make it so that I can โ€œclickโ€ my text?

+6
ios uitableview
source share
3 answers

If you do not need it to be edited, just set your enabled text view type to NO.

+6
source share

For UITextView set textView.userInteractionEnabled = false , and if you have a UITextField , set textField.userInteractionEnabled = false .

If you want textView or textField to be editable after the cell with it is clicked, do something like this:

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) let cell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell // find first textField inside this cell and select it for case let textField as UITextField in cell.subviews { textField.userInteractionEnabled = true textField.becomeFirstResponder() return } // find first textView inside this cell and select it for case let textView as UITextView in cell.subviews { textView.userInteractionEnabled = true textView.becomeFirstResponder() return } } 

Then make sure you turn off user interaction after editing is completed:

  func textFieldDidEndEditing(textField: UITextField) { textField.userInteractionEnabled = false // rest of the function } func textViewDidEndEditing(textView: UITextView) { textView.userInteractionEnabled = false // rest of the function } 

Remember to set UITextFieldDelegate and / or UITextViewDelegate

Hope this helped someone :)

+5
source share

You can assign a delegate to your UITextView, and in the textViewShouldBeginEditing: method, you can manually call the didSelectRowAtIndexPath method. If you cannot easily get the index row of the row to select, you can use a subclass of UITextView with the indexPath property, and in the cellForRowAtIndexPath method: when creating your UITextView, set the indexPath property.

+3
source share

All Articles