Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Selecting through UITextView on custom UITableViewCell

I have a custom UITableViewCell with an image and UITextView property. The textview spans to the edge of the cell. My problem is tapping the textview does not register in didSelectRowAtIndexPath.

How can I make it so that I can "click through" my textview?

like image 876
Oh Danny Boy Avatar asked Oct 28 '10 17:10

Oh Danny Boy


1 Answers

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

If you want the textView or textField to be editable after the cell with it is tapped, 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 to disable user interaction after you finish editing:

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

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

Don't forget to set the UITextFieldDelegate and/or UITextViewDelegate

I hope this helped someone :)

like image 161
budiDino Avatar answered Nov 16 '22 02:11

budiDino