程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了输入文本时 UISearchBar 不搜索大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决输入文本时 UISearchBar 不搜索?

开发过程中遇到输入文本时 UISearchBar 不搜索的问题如何解决?下面主要结合日常开发的经验,给出你关于输入文本时 UISearchBar 不搜索的解决方法建议,希望对你解决输入文本时 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. 当我在我的 `func mealPlansRetrIEved' 中运行 print(self.filteredData) 时,控制台返回我的所有数据,就好像它没有被过滤一样,但是
  2. 一旦我开始在搜索栏中输入,表格视图就不会返回任何单元格,这似乎与上述内容相矛盾

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

extension MealPlanVIEwController: UItableVIEwDelegate,numberOfRowsInSection section: Int) -> Int {
        return mealPlan.count
    }
    
    func tableVIEw(_ tableVIEw: UItableVIEw,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()
    }
}

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

解决方法

Contains 返回布尔值,所以这永远不会失败: item.title?.lowercased().contains(searchText.lowercased())) != nil

要进行检查,您只需删除“!= nil”即可。

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

大佬总结

以上是大佬教程为你收集整理的输入文本时 UISearchBar 不搜索全部内容,希望文章能够帮你解决输入文本时 UISearchBar 不搜索所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: