2016-12-07 3 views
-2

Есть ли способ показать выпадающий список, когда пользователь вводит «@» с определенным ключевым словом поиска в UITextView.Поиск данных из списка при типе пользователя '@' Символ в UITextView

Для примера:

типа «@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 
} 
Смежные вопросы