【发布时间】:2018-05-26 15:47:15
【问题描述】:
我有一个UITableViewCell,我有一个 cmets UITextView 来编写 cmets。在写下 cmets 时,我检查了 #hashtags 和 @mentions 以检查它们在应用程序服务器端的可用性并将它们显示在 tableView 中。
问题是,一旦调用了委托方法,我就无法将其重置以检查其他 if 语句。例如" " 空格字符“如果我以#hashtag 开头我的文本,则永远不会调用If 语句” 所以我不知道它是#hashtag 还是@987654329 的结尾@。还有#hashtag和" "如果我以@mention开始我的文本,则永远不会调用语句
测距和一切都工作得很好,但是当我输入空格时它不会重置以开始新的#hashtag 或@mention
我需要重置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
}
【问题讨论】:
标签: ios swift uitableview uitextview hashtag