2017-12-20 22 views
1

SearchControllerを実装しました。SearchControllerを使用して食品を検索すると、セクション全体が返されます。UISearchControllerは、フィルタ処理されたテーブルビュー項目の代わりにセクションを返します

例えば、私は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

バスが

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

効果的にあなたfoodfilteredFoodが2次元配列されている行です。最初の次元は配列そのものです。 2番目の内部配列foodListFoodオブジェクトの内側にあります。このコードは、このデータ構造の外部次元のみをフィルタリングします。 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

関連する問題