【问题标题】:How to move between textviews inside tableview如何在tableview内的textviews之间移动
【发布时间】:2022-01-06 16:48:21
【问题描述】:

我在 tableview 中有 textview。我想在按下返回键时移动到下一个 textview。现在我可以将 tableview 滚动到下一个索引,但活动 textview 的状态没有更新。

代码如下:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "editorCell", for: indexPath) as? EditorTableViewCell else{return UITableViewCell()}
    cell.textView.delegate = self
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pageCount
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return tableView.bounds.height
}

func textViewDidChange(_ textView: UITextView) {
    
    if textView.text.contains(where: {$0 == "\n"}){
        self.tableView.scrollToRow(at: IndexPath(row: 1, section: 0), at: .top, animated: true)
        self.tableView.layoutIfNeeded()
        self.tableView.reloadData()
    }
}

textViewDidChange 中,我尝试移动到索引 1 处的 textview,但是当我键入字符时,它会写入索引 0 处的 textview。我无法在单元格中切换 textview 的活动状态。

【问题讨论】:

    标签: ios swift text textview


    【解决方案1】:

    您需要保存每一行的文本。

    var text0 = ""
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "editorCell", for: indexPath) as? EditorTableViewCell else{return UITableViewCell()}
        cell.textView.delegate = self
        cell.textView.text = self.text0
        return cell
    }
    
    func textViewDidChange(_ textView: UITextView) {
      self.text0 = textView.text
      ...
    
    

    【讨论】:

      【解决方案2】:

      CellForRow

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
          cell.textView.delegate = self
          cell.textView.text = "helllo \(indexPath.row)"
          cell.textView.tag = indexPath.row
          return cell
      }
      

      UITextViewDelegate

      func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
          if text == "\n" {
              let nextTextViewTag = textView.tag + 1
              for cell in tableView.visibleCells {
                  if let nextTextView = cell.viewWithTag(nextTextViewTag) as? UITextView, let indexPath = tableView.indexPath(for: cell) {
                      nextTextView.becomeFirstResponder()
                      tableView.scrollToRow(at: indexPath, at: .none, animated: true)
                  }
              }
              return false
          }
          return true
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-28
        相关资源
        最近更新 更多