2016-12-07 73 views
-2

当用户在UITextView中输入特定搜索关键字时输入“@”时,是否有任何显示下拉列表的方法?当用户输入“@”时从列表中搜索数据UITextView中的符号

例如:

类型“@ios”和所有包含在下拉列表列表中显示单词“IOS”过滤字符串。

+0

查看以下链接并根据您的要求进行修改。 https://github.com/EddyBorja/MLPAutoCompleteTextField https://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values – Venkat

回答

0

我通过不断地检查用户键入的文本来实现这一点。

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 
} 
0

我认为你正在寻找的东西像this.This代表将被称为每当用户按下按键@

试试这个: -

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 
}