代码之家  ›  专栏  ›  技术社区  ›  Oh Danny Boy

iOS:在自定义UITableViewCell上通过UITextView进行选择

  •  6
  • Oh Danny Boy  · 技术社区  · 15 年前

    我有一个自定义的UITableViewCell,它具有Image和UITextView属性。textView跨越到单元格的边缘。我的问题是点击textView未在didselectrowatindexpath中注册。

    我怎样才能让它“点击”我的文本视图?

    3 回复  |  直到 8 年前
        1
  •  6
  •   jer    15 年前

    如果不需要它可编辑,只需设置文本视图的 enabled 属性到编号

        2
  •  6
  •   budiDino    8 年前

    为了 UITextView 设置 textView.userInteractionEnabled = false 如果你有 UITextField ,集合 textField.userInteractionEnabled = false .

    如果希望在点击带有文本视图或文本字段的单元格后对其进行编辑,请执行以下操作:

    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

    我希望这对某人有帮助:)

        3
  •  3
  •   GendoIkari    15 年前

    可以将委托分配给uitextview,在其textviewshouldbeginediting:方法中,可以手动调用didselectrowatindexpath方法。如果无法轻松获取要选择的行的indexPath,则可以使用具有indexPath属性的uitextView子类,并在cellForRowatindexPath:方法中,创建uitextView时,设置indexPath属性。