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.nnPlease 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()
}

可以根据您的问题在这里集成简单的逻辑

var isSearchActive = false // bool variable
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
if self.searchBar.text == ""
{
isSearchActive = false 
//--               
}
else
{
isSearchActive = true
}
self.tableView.reloadData()
}
//logic  this  data source method
func numberOfSections(in tableView: UITableView) -> Int
{
if coreDataVariable.count > 0 {
self.tableView.backgroundView = nil
self.tableView.separatorStyle = .singleLine
return 1
}
else 
{ 
if isSearchActive == true
{
// -- Make UIlabel here
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection  section: Int) -> Int
{
return coreDataVariable.count
}

当没有数据时,您的coreDataVariable.count似乎为0。因此,没有行,并且您的标签显示了它被告知显示的内容"未找到查询。\n\n请检查您的搜索。"我相信coreDataVariable在被搜索之前包含了所有数据。您还需要一些经过排序筛选的Dat

看不到你的搜索条形码:

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)

确保您的类符合UISearchResultsUpdatingUISearchBarDelegate此外,这个可能会有所帮助https://stackoverflow.com/a/28997222/1200543

最新更新