2017-12-20 32 views
1

SearchController을 구현했으며 SearchController을 사용하여 음식 항목을 검색하면 전체 섹션을 반환합니다.UISearchController는 필터링 된 tableview 항목 대신 섹션을 반환합니다.

예를 들어, 나는 SearchController를 사용하여 Chocolate를 검색하고 그 대신 Chocolate의, Sweets의 전체 섹션을 표시하는있는 tableView에 돌아가 초래, 그것은뿐만 아니라 Lollipops를 반환 할 수 있습니다. 나는 여기서 무엇을 잘못하고 있니? 나는 상당히 빨리 새로운 도움이 될 것입니다. 고맙습니다.

struct Food { 
    let foodTaste: String 
    let foodList: [String] 
} 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating { 

    func updateSearchResults(for searchController: UISearchController) { 
     if searchController.searchBar.text! == "" { 
      filteredFood = food 
     } else { 
      filteredFood = food.filter { $0.foodList.contains(where: { $0.contains(searchController.searchBar.text!) }) } 
      self.tableView.reloadData() 
     } 
    } 

    var tableView = UITableView() 
    var food = [Food]() 
    var filteredFood = [Food]() 

    let searchController = UISearchController(searchResultsController: nil) 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     filteredFood = food 
     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = false 
     tableView.tableHeaderView = searchController.searchBar 
     definesPresentationContext = true 
     tableView.frame = self.view.frame 
     self.view.addSubview(tableView) 

     tableView.delegate = self 
     tableView.dataSource = self 

     let sweets = Food(foodTaste: "Sweet", foodList: ["Chocolate", 
     "Lollipops"]) 
     let sour = Food(foodTaste: "Sour", foodList: ["Lemon", "Limes"]) 
     food.append(sweets) 
     food.append(sour) 
    } 


    func numberOfSections(in tableView: UITableView) -> Int { 
     return filteredFood.count 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return filteredFood[section].foodList.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: nil) 
     cell.textLabel?.text = filteredFood[indexPath.section].foodList[indexPath.row] 
     return cell 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return filteredFood[section].foodTaste 
    } 

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return 4 
    } 


} 

답변

1

버스 효과적으로 foodfilteredFood가 2 차원 배열되는 선

 filteredFood = food.filter { $0.foodList.contains(where: { $0.contains(searchController.searchBar.text!) }) } 

이다. 첫 번째 차원은 배열 자체입니다. Food 오브젝트 내의 2 번째의 내부 배열 foodList. 이 코드는이 데이터 구조의 외부 차원 만 필터링합니다. foodList 안에 검색과 일치하는 문자열이 있으면 Food 개체 전체가 필터를 통과해야합니다. 이것이 원하는 것이 아니라면 새로운 Food 개체를 만들어야합니다. 아마도 다음과 같은 것일 수 있습니다 :

 filteredFood = food.filter { $0.foodList.contains(where: { $0.contains(searchController.searchBar.text!) }) } 
          .map { Food(foodTaste: $0.foodTaste, foodList: $0.foodList.filter({$0.contains(searchController.searchBar.text!) }) } 
+0

감사합니다. – Mau