2016-12-07 4 views
-2

UITextView에 특정 검색 키워드를 사용하여 사용자가 "@"을 입력 할 때 드롭 다운 목록을 표시 할 수있는 방법이 있습니까? 예를 들어사용자 유형이 '@'일 때 목록에서 데이터 검색 UITextView의 기호

:

유형 "@ios"하고 드롭 다운 목록에서 목록 쇼에서 단어 "IOS"필터가 포함 된 모든 문자열입니다.

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

내가 지속적으로 사용자 유형의 텍스트를 확인하여이 달성 - :

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 
}