iOS:通过定制UITableViewCell上的UITextView进行select

我有一个图像和UITextView属性的自定义UITableViewCell。 文本视图跨越到单元格的边缘。 我的问题是攻丝textview不注册在didSelectRowAtIndexPath。

我怎样才能让我可以“点击”我的textview?

如果你不需要它是可编辑的,只要将你的文本视图的enabled属性设置为NO。

对于UITextView设置textView.userInteractionEnabled = false ,如果你有UITextField ,设置textField.userInteractionEnabled = false

如果您希望textView或textField在单元格被点击后可编辑,请执行如下操作:

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

然后确保在完成编辑后禁用用户交互:

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

不要忘记设置UITextFieldDelegate和/或UITextViewDelegate

我希望这有助于人:)

您可以将委托分配给您的UITextView,并在其textViewShouldBeginEditing:方法中,您可以手动调用didSelectRowAtIndexPath方法。 如果无法轻松获取要select的行的indexPath,则可以使用具有indexPath属性的UITextView的子类,并在cellForRowAtIndexPath:方法中创buildUITextView时,设置indexPath属性。