ios - Swift - 没有数据时 UISearchBar 不显示结果消息
问题描述
我有一个UISearchBar过滤器,它运行良好,可以从我的CoreData数据库中正确找到我的UITableView中的数据。
当它没有找到匹配的文本时它也会正确显示,然后在匹配时消失并显示数据。
我的问题是当表格视图中没有数据时显示无结果消息。
我知道问题的原因,因为我的代码在计数大于 0 时不返回消息,并且由于表中没有数据,它肯定会显示消息,但我不知道如何解决这个问题。
有人可以指导我一种方法,以便仅在搜索文本后才弹出无结果消息,然后在不使用搜索栏时关闭?
我还检查了 Stack Overflow 上的无数相关问题,但没有一个能解决我的需求。
截屏
如您所见,它甚至在没有使用UISearchBar的情况下显示消息。
代码
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if coreDataVariable.count > 0 {
self.tableView.backgroundView = nil
self.tableView.separatorStyle = .singleLine
return 1
}
let rect = CGRect(x: 0,
y: 0,
width: self.tableView.bounds.size.width,
height: self.tableView.bounds.size.height)
let noDataLabel: UILabel = UILabel(frame: rect)
noDataLabel.numberOfLines = 0
noDataLabel.text = "Query not found.\n\nPlease check your search."
noDataLabel.textAlignment = NSTextAlignment.center
self.tableView.backgroundView = noDataLabel
self.tableView.separatorStyle = .none
return coreDataVariable.count
}
这是我的搜索栏功能
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if !searchText.isEmpty
{
var predicate: NSPredicate = NSPredicate()
predicate = NSPredicate(format: "attriubteName1 contains[c] '\(searchText)' OR attriubteName2 contains[c] '\(searchText)'")
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EntityName")
fetchRequest.predicate = predicate
do
{
coreDataVariable = try managedObjectContext.fetch(fetchRequest) as! [NSManagedObject] as! [ObjectName]
}
catch let error as NSError
{
let alert = UIAlertController(title: "Cannot Search Data", message: "Error searching saved data. Please reinstall ObjectName Saver if problem persists.", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)
{
UIAlertAction in
alert.removeFromParent()
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
print("Could not fetch. \(error)")
}
}
else
{
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ObjectName")
do
{
coreDataVariable = try managedObjectContext.fetch(fetchRequest) as! [ObjectName]
}
catch
{
let alert = UIAlertController(title: "Cannot Load Data", message: "Error loading saved data. Please app if problem persists.", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) {
UIAlertAction in
alert.removeFromParent()
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
print("Error loading data")
}
}
table.reloadData()
}
解决方案
没有数据时,您的 coreDataVariable.count 似乎为 0。因此没有行,您的标签显示了它被告知显示“未找到查询。\n\n请检查您的搜索。” 我相信 coreDataVariable 在被搜索之前包含所有数据。您还需要一些排序的过滤数据
没有看到您的 searchBar 代码:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if searchController.isActive && searchController.searchBar.text != ""{
// check your filtered data count here and if it is 0, show your label
// Do the logic and find how many rows you need
}
}
经验法则,当 tableView 有 SearchBar 时 NumberOfSection、NumebrOfRow、CellForRow 需要:
if searchController.isActive && searchController.searchBar.text != ""{
//Your logic here
}
2019 年 11 月 7 日更新: 代码中的某处定义了这个
let searchController = UISearchController(searchResultsController: nil)
确保您的课程符合UISearchResultsUpdating
并且UISearchBarDelegate
这也可以帮助
https://stackoverflow.com/a/28997222/1200543
推荐阅读
- javascript - Vue方法下载属性不起作用它重定向到url
- function - 如何在酶或开玩笑测试中模拟外部函数
- c# - 更新基本控制器变量值,但在 ASP.NET Core 中未更新所有派生控制器
- node.js - 从 body-parser 传递参数以获取 JSON 数据的问题
- android - 仅在 recyclerview 上的滚动条边距(而不是 recyclerview)
- r - R 如果三个或更多值是 NA,则删除行
- .net - 当流调用循环很大时,Grpc 双向流被对等端终止?
- php - 使用 livewire 从 URL 获取类型参数值
- reactjs - React路由器:使用url参数重定向?
- docker - nginx server block + docker,前后如何通信