如何解决委托方法,textViewDidChangeSelection 不是每次都触发
我在 UITableView 单元格中嵌入了一个 UITextView,CustomCell
。如果我在光标位于文本末尾时点击返回键,我想在该单元格下方创建一个新单元格,但是如果我在 textView 中间时点击返回键,我想获得其余的光标后的单词并在 textView 中创建一个包含该文本的单元格。
为此,我需要知道在 UITextViewDelegate
方法 textViewDidChangeSelection(_:)
的帮助下获得的 textView 中的光标位置。
它仅在我点击同一单元格上的不同位置时才有效,但如果我移动到不同的单元格并再次点击同一位置的先前点击的单元格(在文本视图中),则不会触发委托方法.
让我用一个例子来展示这个案例-
我输入了一些文本进行测试。在这里,在第 5 行之后,我决定在“juice”这个词之前点击第 2 行 -
委托方法 textViewDidChangeSelection(_:)
被触发。
现在,我点击整个文本“脱脂牛奶”后的第 3 行喜欢-
再次调用委托方法 textViewDidChangeSelection(_:)
。
但是现在当我移回第二行的相同位置时,意思是在“juice”这个词之前,委托方法不会被触发。同样,如果我在“milk”这个词之后的相同位置点击第三行,则不会调用委托方法。
我想,原因是在那些单元格中,我点击了理智的位置,因为选择没有改变,所以没有调用委托方法。
以下是 CustomCell
(UITableViewCell) 类中的委托方法的代码-
func textViewDidChangeSelection(_ textView: UITextView){
if let range = textView.selectedTextRange,let indexPath = currentIndexPath{
let curserStartPosition = textView.offset(from: textView.beginningOfDocument,to: range.start)
setActiveCell?(indexPath,curserStartPosition)
}
}
以下是Controller的代码,当点击返回键时创建新单元格-
class MyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var cellCount = 1
var initialCellText: String?
var activeCell: CustomCell?
var cursorPosition: Int?
@IBOutlet weak var myTableView: UITableView!{
didSet{
myTableView.delegate = self
myTableView.dataSource = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func goToNextCell(_ indexPath:IndexPath){
let nextCelIndexPath = IndexPath(row: indexPath.row + 1,section: 0)
if let text = activeCell?.myTextView.text,let cursorPos = cursorPosition,cursorPos < text.count-1 {
let fromIndex = text.index(text.startIndex,offsetBy: cursorPos)
let toIndex = text.index(text.endindex,offsetBy: -1)
if fromIndex < toIndex {
let truncatedText = text[fromIndex...toIndex]
self.initialCellText = String(truncatedText)
}
}
cellCount += 1
myTableView.beginUpdates()
myTableView.insertRows(at: [nextCelIndexPath],with: .none)
myTableView.endUpdates()
initialCellText = nil
cursorPosition = nil
if let cell = self.myTableView.cellForRow(at: nextCelIndexPath) as? CustomCell{
self.activeCell = cell
self.activeCell?.myTextView.becomeFirstResponder()
}
}
func setActiveCell(on indexPath: IndexPath,at curserPos: Int){
if let tappedOnCell = myTableView.cellForRow(at: indexPath) as? CustomCell{
self.activeCell = tappedOnCell
self.cursorPosition = curserPos
}
}
}
// MARK: - UITableViewDelegate and UITableViewDataSource
extension MyViewController{
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return cellCount
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomCell{
cell.myTextField.becomeFirstResponder()
cell.returnKeyTapped = goToNextCell(_:)
cell.setActiveCell = setActiveCell(on:at:)
return cell
}
return UITableViewCell()
}
}
要创建以下单元格,我需要每次都知道光标位置在特定单元格上的位置。所以,我需要每次都触发委托方法。
任何人都可以就我如何解决我的问题提出任何建议。
解决方法
如果我理解正确的话,您基本上想在输入 UITextView
后知道光标位置。
现在,如果您输入 UITextView
,该文本视图将成为第一响应者,并开始编辑会话。这是由 UITextView.textDidBeginEditingNotification
宣布的,您可以为其添加观察者,请参阅 here。
在通知调用的函数中,您可以像在 textViewDidChangeSelection
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。