Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何显示搜索结果,如果没有结果,如何显示无结果文本?_Swift_Uisearchcontroller_Searchbar - Fatal编程技术网

Swift 如何显示搜索结果,如果没有结果,如何显示无结果文本?

Swift 如何显示搜索结果,如果没有结果,如何显示无结果文本?,swift,uisearchcontroller,searchbar,Swift,Uisearchcontroller,Searchbar,我试图只显示结果,而不是来自解析服务器的所有数据。我尝试了不同的方法(基本相同),但失败了。我知道我错过了一些东西 我的问题是,如果没有结果,如何在屏幕上仅显示结果以及无结果文本 你可以在下面找到我的方法 事先非常感谢 类SearchTableViewController:UITableViewController、UISearchBarDelegate、UISearchResultsUpdate{ @ibvar搜索栏:UISearchBar! var searchActive:Bool=fa

我试图只显示结果,而不是来自解析服务器的所有数据。我尝试了不同的方法(基本相同),但失败了。我知道我错过了一些东西

我的问题是,如果没有结果,如何在屏幕上仅显示结果以及无结果文本

你可以在下面找到我的方法

事先非常感谢

类SearchTableViewController:UITableViewController、UISearchBarDelegate、UISearchResultsUpdate{
@ibvar搜索栏:UISearchBar!
var searchActive:Bool=false
变量数据:[PFObject]!
var noteObjects:NSMutableArray!=NSMutableArray()
已筛选变量:[PFObject]!
变量刷新器:UIRefreshControl=UIRefreshControl()
var searchController=UISearchController()
重写func viewDidLoad(){
super.viewDidLoad()
搜索()
searchBar.delegate=self
self.refresher.addTarget(self,操作:#选择器(刷新),for:.valueChanged)
self.tableView?.addSubview(刷新器)
self.searchDisplayController?.searchResultsTableView.rowHeight=tableView.rowHeight;
}
@objc func refresh(){
打印(“刷新”)
self.refresh.endRefreshing()
搜索()
}
@objc func搜索(searchText:String?=nil){
let query=PFQuery(类名:“NewUsers”)
query.whereKey(“user”,equalTo:PFUser.current()如有)
如果(searchText!=nil){
query.whereKey(“名称”,包含:searchText)
}
query.findObjectsInBackground{(对象,错误)位于
如果错误==nil{
如果让汽车=物体{
汽车中的物体{
如果(对象作为PFObject?)=nil{
self.data=对象
self.tableView.reloadData()
}
}
}
}
}
}
func updateSearchResults(对于searchController:UISearchController){
self.filtered.removeAll(保留容量:false)
让searchText=searchController.searchBar.text
let query:PFQuery=PFQuery(类名:“NewUsers”)
如果searchController.isActive==true{
query.whereKey(“name”,matchesRegex:searchText!,修饰符:“i”)
self.tableView.reloadData()
}
query.findObjectsInBackground{(结果,错误)位于
self.filtered=结果
self.tableView.reloadData()
}
打印(如有搜索文本)
}
var应使用搜索结果:Bool{
如果让searchText=searchController.searchBar.text{
如果searchText.i为空{
返回错误
}
}
return searchController.isActive
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//标记:-表视图数据源

重写func numberOfSections(在tableView:UITableView中)->Int{ //#警告未完成执行,返回节数 返回1 } 重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{ 如果数据!=nil{ 返回self.data.count } 返回0 } 重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{ 让cell=self.tableView.dequeueReusableCell(标识符为“cell”,for:indexath)作为!SearchTableViewCell 设obj=self.data[indexPath.row] self.name=(obj[“name”]作为?字符串)! self.lasname=(obj[“姓氏”]作为?字符串)! self.birthdate=(obj[“birthday”]作为?字符串)! self.age=(obj[“age”]作为?字符串)! self.city=(obj[“city”]as?String)! cell.dateLabel.text=“:”+生日 cell.ageLabel.text=“:\(年龄)” cell.userLabel.text=“:”+姓名+“”+姓氏 返回单元 } func searchBarTextDidBeginEditing(searchBar:UISearchBar){ searchActive=true; } func SearchBartextdidediting(searchBar:UISearchBar){ searchActive=false; } func searchBarCancelButtonClicked(searchBar:UISearchBar){ searchActive=false; self.searchBar.endEditing(真) } func searchBar searchbutton单击(searchBar:UISearchBar){ searchActive=false; self.searchBar.endEditing(真) } func searchBar(searchBar:UISearchBar,textDidChange searchText:String){ 搜索(searchText:searchText) } func textField应返回(textField:UITextField)->Bool{ textField.resignFirstResponder()辞职 返回真值 } 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ self.view.endEditing(true) self.searchBar.resignFirstResponder()辞职 } }
self.arraydalpage
array
self.dealsTable
tableView
希望,它可能有助于u和
delegate
datasource
协议的添加


self.arraydalpage
array
self.dealsTable
tableView
希望,它可能有助于u和
delegate
以及添加的
datasource
协议。只需检查searchactive和data count是否为0,然后通过返回tableView的1个计数,在标签或单元格中显示无结果文本
func numberOfSections(in tableView: UITableView) -> Int {

    if (self.data.count == 0)
    {
        let noDataLabel: UILabel     = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: tableView.bounds.size.height))
        noDataLabel.text          = "No Result Found."
        noDataLabel.textColor     = UIColor.red
        noDataLabel.textAlignment = .center
        self.tableView.backgroundView  = noDataLabel
        self.tableView.backgroundColor = UIColor.white
        self.tablevİEW.separatorStyle  = .none
    }
        return 1

}