Swift - UISearchBar 在没有数据时不显示结果消息
Swift - UISearchBar no result message shows up when there is no data
我有一个 UISearchBar 过滤器,它运行良好,可以在我的 UITableView 正确地从我的 CoreData 数据库。
它在找不到匹配的文本时也会正确显示,然后在匹配并显示数据时消失。
我的问题是,当 Table 视图中没有数据时,会显示无结果消息。
我知道问题的原因,因为我的代码在计数大于 0 时不会 return 消息,并且由于 table 中没有数据,它肯定会显示消息,但我不知道如何解决。
有人可以指导我如何让无结果消息 仅在搜索文本后弹出 然后在不使用搜索栏时消失吗?
我还在 Stack Overflow 上检查了无数相关问题,但 none 满足了我的需求。
截图
Picture of the issue
如您所见,它显示的消息甚至没有使用 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。因此没有行,你的标签显示了它被告知要显示的内容 "Query not found.\n\nPlease check your search." 我相信 coreDataVariable 包含搜索之前的所有数据。您还需要一些已排序的 filteredDat
没有看到您的搜索条码:
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)
确保您的 class 符合 UISearchResultsUpdating
和 UISearchBarDelegate
这也可能有帮助
这里可以根据你的问题综合简单的逻辑
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
}
我有一个 UISearchBar 过滤器,它运行良好,可以在我的 UITableView 正确地从我的 CoreData 数据库。
它在找不到匹配的文本时也会正确显示,然后在匹配并显示数据时消失。
我的问题是,当 Table 视图中没有数据时,会显示无结果消息。
我知道问题的原因,因为我的代码在计数大于 0 时不会 return 消息,并且由于 table 中没有数据,它肯定会显示消息,但我不知道如何解决。
有人可以指导我如何让无结果消息 仅在搜索文本后弹出 然后在不使用搜索栏时消失吗?
我还在 Stack Overflow 上检查了无数相关问题,但 none 满足了我的需求。
截图
Picture of the issue
如您所见,它显示的消息甚至没有使用 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。因此没有行,你的标签显示了它被告知要显示的内容 "Query not found.\n\nPlease check your search." 我相信 coreDataVariable 包含搜索之前的所有数据。您还需要一些已排序的 filteredDat
没有看到您的搜索条码:
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)
确保您的 class 符合 UISearchResultsUpdating
和 UISearchBarDelegate
这也可能有帮助
这里可以根据你的问题综合简单的逻辑
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
}