私はUITableViewCell
を持っています。私はコメントを書くためにUITextView
のコメントがあります。コメントを書き留めている間、私は#hashtags
と@mentions
をチェックして、アプリのサーバー側で可用性を確認し、tableView
に表示します。UITextViewDelegate ShouldTextChangeInを呼び出す際に奇妙な動作が発生する
デリゲートメソッドが呼び出されると、それをリセットして他のif文をチェックインできないという問題があります。たとえば、" "
の空白文字「#hashtag
でテキストを開始した場合、If文が呼び出されることはありません」です。したがって、#hashtag
または@mention
の末尾かどうかはわかりません。私は@mention
で私のテキストを開始した場合も#hashtag
と" "
文が呼び出されることはありませんしている場合至るまで、すべてが完璧に正常に動作しているが、私はスペースを入力するときには、新しい#hashtag
か@mention
ドを開始するresetingされていません私はrange
をリセットする必要がありますか、何をする必要がありますか?
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if textView == postCommentTextView {
if textView.text == "#" {
recordingHash = true
recordingMention = false
startParseHash = range.location
} else if textView.text == "@" {
recordingHash = false
recordingMention = true
startParseMention = range.location
} else if textView.text == " " {
recordingHash = false
recordingMention = false
createdHashesArray.append(createdHashString)
createdHashString = ""
}
if recordingHash != nil {
if recordingHash == true {
var value = String()
print("COUNT: \(textView.text.count)")
if startParseHash <= (textView.text.count - startParseHash) {
let createdRange = NSMakeRange(startParseHash, textView.text.count - startParseHash)
value = textView.text(in: createdRange.toTextRange(textView)!)! //as! NSString
createdHashString = "\(value)"
// print("hash:\(createdHashString)")
}
print("hash:\(createdHashString)")
if textView.text.count == 0 {
recordingHash = false
recordingMention = false
// createdHashesArray.append(createdHashString)
createdHashString = ""
}
}
}
return true
}
Yess ...私は文字の 'value'をチェックしそこから行く必要があります。 –
コードは本当に完全に正しく再実行する必要があります。ユーザーはテキストを貼り付けることができるので、一度に1つの文字だけを入力するとは限りません。もちろん、ユーザーは削除することもできます。最後に、ユーザーがテキストを選択してから入力または貼り付けできることを忘れないでください。 – rmaddy