【问题标题】:How to make a UITextView detect the click on table cell如何让 UITextView 检测到点击表格单元格
【发布时间】:2019-10-22 11:32:16
【问题描述】:

我正在处理一个项目,其中一个表格视图的文本是 UITextView,但另一个是 UILabel。 UILabel 将用户的点击检测为对表格单元格的点击,但 UITextView 没有。为什么会这样?有什么办法可以解决吗?

【问题讨论】:

  • 您是否检查过UITextView isUserInteraction 已启用?
  • 你的UITextVieweditable是吗?
  • 检查是否有其他视图覆盖textView。另外,检查您是否对textView 施加了适当的约束。
  • 您预计会发生什么?点击UITextView 将开始编辑该文本视图。如果不是这样,你希望你的代码做什么?
  • 我希望它检测到点击,所以当我点击它时要选择的单元格

标签: ios swift uitableview uitextview


【解决方案1】:

试试看

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

}

【讨论】:

    【解决方案2】:

    确保 textView 的 isSelectable 属性为 true。

    【讨论】:

      【解决方案3】:

      我弄明白了,cell view和text view都是scrow view,总是会有冲突,所以我需要把text view的UserInteractions和Multiple Touch的勾去掉,booth设置为false

      【讨论】:

      • 不,这与滚动视图无关。
      【解决方案4】:

      我不确定您是在谈论UITableView 行还是UITextView/UILabel 本身的选择。

      如果您尝试在 UILabelUITextView 上设置手势识别器,我想知道这是否有必要,或者您是否可以在视图控制器中使用 didSelectRowAtIndexPath

      例如

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

      然后您将在yourMethodHere() 中定义当单元格被选中时您希望发生的行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-24
        • 2017-06-13
        • 2014-05-16
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        相关资源
        最近更新 更多