我在 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 的活動狀態。
uj5u.com熱心網友回復:
您需要為每一行保存文本。
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
...
uj5u.com熱心網友回復:
單元格換行
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370552.html
下一篇:更改初始螢屏
