首页 > 解决方案 > 输入文本时 UISearchBar 不搜索

问题描述

我有一个工作正常的表视图。但是,当我尝试实现 UISearchBar 并显示过滤后的数据时,没有任何内容被过滤。这是我的视图控制器:


import UIKit

class MealPlanViewController: UIViewController, UISearchBarDelegate {
    
    private var model = MealPlanModel()
    private var mealPlan = [MealPlan]()
    var filteredData: [MealPlan]!
        
    @IBOutlet weak var topBarStackView: UIStackView!
    
    @IBOutlet weak var searchBar: UISearchBar!
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        model.delegate = self
        
        searchBar.delegate = self
        
        filteredData = mealPlan
    }
    
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        
        filteredData = []
        
        if searchText == "" {
            filteredData = mealPlan
        }
        else {
            
            for item in mealPlan {
                
                if ((item.title?.lowercased().contains(searchText.lowercased())) != nil) {

                    filteredData.append(item)
                }
            }
        }
        
        self.tableView.reloadData()
        
    }
}

extension MealPlanViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell", for: indexPath) as! MealPlanCell
        
        let filteredMealPlaninTable = filteredData[indexPath.row]
        
        cell.displayMealPlan(filteredMealPlaninTable)
                
        return cell
    }
        
}

extension MealPlanViewController: MealPlanProtocol {
    
    func mealPlansRetrieved(mealPlans: [MealPlan]) {
        
        self.filteredData = mealPlans
        
        tableView.reloadData()
        
    }
}

几点注意事项:

  1. 当我print(self.filteredData)在我的“func mealPlansRetrieved”中运行时,控制台会返回我的所有数据,就好像它没有被过滤一样,但是
  2. 一旦我开始在搜索栏中输入,表格视图不会返回任何单元格,这似乎与上述矛盾

作为参考,这是过滤之前的代码,它确实有效:

extension MealPlanViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mealPlan.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell", for: indexPath) as! MealPlanCell
        
        let mealPlanInTable = mealPlan[indexPath.row]
        
        cell.displayMealPlan(mealPlanInTable)

        return cell
    }
}

extension MealPlanViewController: MealPlanProtocol {
    
    func mealPlansRetrieved(mealPlans: [MealPlan]) {
        
        self.mealPlan = mealPlans
        
        tableView.reloadData()
    }
}

非常感谢任何帮助/指导!

标签: iosswiftfirebaseuitableviewuisearchbar

解决方案


包含返回布尔值,因此这永远不会失败: item.title?.lowercased().contains(searchText.lowercased())) != nil

要进行检查,您可以简单地删除“!= nil”。

我不确定您从哪里调用mealPlansRetrieved,但您可能希望保留“self.mealPlan = mealPlans”这一行而不是“self.filteredData = mealPlans”。


推荐阅读