2017-10-26 2 views
0
나는 부분

배열 하위

struct words { 

    var sectionName : String! 
    var coptic : [String]! 
    var english : [String]! 
} 

var array = [words]() 
var filtered = [words]() 

array = [words(sectionName: "", coptic: [""], English: [""])] 
나는 불행하게도이

func updateSearchResults(for searchController: UISearchController) { 
    // If we haven't typed anything into the search bar then do not filter the results 
    if searchController.searchBar.text! == "" { 
     filtered = array 
    } else { 
     // Filter the results 
     filtered = array.filter { $0.coptic.lowercased().contains(searchController.searchBar.text!.lowercased()) } 

    } 

유사한 코드를 사용하여 searchcontroller을 활용하려는

으로있는 tableView를 만들려면 다음 배열 구조를 사용하고

와 UISearchBarController을 사용하는 방법, 콥트어는 단순히 String이 아닌 [String]이므로 코드가 작동하지 않습니다. 콥틱 하위 섹션에 대한 검색을 필터링 할 수 있도록 수정하는 방법이 있습니까?

답변

1

이렇게 할 수 있습니다.

func updateSearchResults(for searchController: UISearchController) { 
    // If we haven't typed anything into the search bar then do not filter the results 
    if searchController.searchBar.text! == "" 
    { 
     filtered = array 
    } 
    else 
    { 
     filtered.removeAll() 
     array.forEach({ (word:words) in 

      var tempWord:words = words.init(sectionName: word.sectionName, coptic: [""], english: [""]) 
      let copticArray = word.coptic.filter({ (subItem:String) -> Bool in 
        let a = subItem.lowercased().contains(searchController.searchBar.text!.lowercased()) 
        return a; 
       }) 
      tempWord.coptic = copticArray 
      filtered.append(tempWord) 
     }) 

    } 
} 

입력 배열 = 배열 ​​= [워드 (sectionName "ABC"콥트 [ "애플", "공", "고양이", "개」, 영어 [", "]) ] "응용 프로그램"

출력 단어를

검색 (sectionName : abc 방송, 콥트는 : "사과"], 영어 : [ ""])]

+0

내가 뭘하려하면 제안 된 그러나 그것은 작동하지 않았다. –

+0

위의 문제에 직면 한 코드는 –

+0

입니다. 이전에 같은 질문을했는데 아무도 응답하지 않았습니다. 내가 사용하고있는 전체 코드로 링크를 첨부하고 있습니다. 도움이된다면 가장 감사 할 것입니다. 고맙습니다. https://stackoverflow.com/questions/46854927/uisearchbar-of-an-string-within-an-array –