ios - 输入文本时 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()
}
}
几点注意事项:
- 当我
print(self.filteredData)
在我的“func mealPlansRetrieved”中运行时,控制台会返回我的所有数据,就好像它没有被过滤一样,但是 - 一旦我开始在搜索栏中输入,表格视图不会返回任何单元格,这似乎与上述矛盾
作为参考,这是过滤之前的代码,它确实有效:
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()
}
}
非常感谢任何帮助/指导!
解决方案
包含返回布尔值,因此这永远不会失败: item.title?.lowercased().contains(searchText.lowercased())) != nil
要进行检查,您可以简单地删除“!= nil”。
我不确定您从哪里调用mealPlansRetrieved,但您可能希望保留“self.mealPlan = mealPlans”这一行而不是“self.filteredData = mealPlans”。
推荐阅读
- delphi - Delphi - TFDQuery - 如何设置 updateStatus usUnmodified 仅用于一条记录而不还原它?
- networking - 如何使用ip地址查找位置
- flutter - 如何将数据从 StatelessWidget 传递和接收到另一个?
- c# - Xamarin.Forms - DatePicker 复制
- c++ - 如何遍历文本文件中的行并将其与其他行进行比较(C++)
- ruby-on-rails - 尝试在 Rails 上实现搜索功能时获取“表单中的第一个参数不能包含 nil 或为空”
- java - 我不能使用正则表达式替换包含“\”字符的文本
- sql - 左连接不返回任何行
- javascript - 如何在已设置命令处理程序的 discord.js 上使用参数
- python - 如何切片直到最后一项以形成新列?