Ios 用户在UITextView中键入“@”符号时从列表中搜索数据
当用户在UITextView中使用特定搜索关键字键入@时,是否有任何方法显示下拉列表 例如:Ios 用户在UITextView中键入“@”符号时从列表中搜索数据,ios,ios8,swift2,Ios,Ios8,Swift2,当用户在UITextView中使用特定搜索关键字键入@时,是否有任何方法显示下拉列表 例如: 键入@ios和包含列表中的word ios筛选器并显示在下拉列表中的所有字符串。我想您正在寻找类似的内容。每当用户按@key时,将调用此委托 试试这个:- func textView(_ textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
键入@ios和包含列表中的word ios筛选器并显示在下拉列表中的所有字符串。我想您正在寻找类似的内容。每当用户按@key时,将调用此委托 试试这个:-
func textView(_ textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (textView.text.appending(text) == "@") {
//trigger '@' operation
if arrayObject.contains(value) {
//Populate array and display tableview.
}
}
else if (textView.text.appending(text) == "/") {
//trigger / operation
}
//Same conditions go on
}
我通过不断检查用户类型文本来实现这一点
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// if user type any latter from @,space and new line then clear the search string
if text == "@" || text == " " || text == "\n" {
self.searchString = ""
}
// append the type string in text view text
let resultString = textView.text.stringByAppendingString(text)
// break the line with @ symbol and find the occurence
let numberOfOccurrences = resultString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "@")).count - 1
// store all words for checking
let stringArray = resultString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "@")) ?? [String]()
// checking if string array > 1 that means string contains @ symbol
self.searchString = stringArray.count > 1 ? stringArray[numberOfOccurrences].containsString(" ") ? "" : stringArray[numberOfOccurrences] : ""
if self.searchString.characters.count > 0 {
self.getTopSearchFromLive(self.searchString)
} else {
self.tableView.hidden = true
}
return true
}
请查看以下链接,并根据您的要求进行修改。