Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Ios 如果TableView没有结果,则显示“;“没有结果”;标签(2个)_Ios_Swift_Xcode_Uitableview_Count - Fatal编程技术网

Ios 如果TableView没有结果,则显示“;“没有结果”;标签(2个)

Ios 如果TableView没有结果,则显示“;“没有结果”;标签(2个),ios,swift,xcode,uitableview,count,Ios,Swift,Xcode,Uitableview,Count,我正在尝试完成我的搜索功能,这是我唯一缺少的东西。基本上,我想要的是当我的searchTableView没有任何结果时,一个标签会出现并说“没有结果”,就像WhatsApp: 我知道如何设置标签,并在没有需要一个计数的结果时使其正确显示,但当没有需要等于0的2个计数的结果时,我不知道如何将标签正确设置为ishiden 让我解释一下: 行数部分: override func tableView(_ tableView: UITableView, numberOfRowsInSection sec

我正在尝试完成我的搜索功能,这是我唯一缺少的东西。基本上,我想要的是当我的
searchTableView
没有任何结果时,一个标签会出现并说“没有结果”,就像WhatsApp

我知道如何设置标签,并在没有需要一个
计数的结果时使其正确显示,但当没有需要等于
0
的2个
计数的结果时,我不知道如何将标签正确设置为
ishiden

让我解释一下: 行数部分:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == self.tableView {
            
        return self.names.count
            
        } else {
            return self.filteredUsers.count
        }
        
    }
BarsSearchNoResultsLabel = UILabel(frame: CGRect(x: 60, y: 70, width: 200, height: 50))
        BarsSearchNoResultsLabel.textAlignment = .center
        BarsSearchNoResultsLabel.text = "אין תוצאות"
        BarsSearchNoResultsLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 25.0)
        BarsSearchNoResultsLabel.textColor = UIColor.lightGray
        self.view.addSubview(BarsSearchNoResultsLabel)
我需要
self.names.count
self.filteredUsers.count
都等于0,然后我将写入
ishiden=false

我拥有的标签:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == self.tableView {
            
        return self.names.count
            
        } else {
            return self.filteredUsers.count
        }
        
    }
BarsSearchNoResultsLabel = UILabel(frame: CGRect(x: 60, y: 70, width: 200, height: 50))
        BarsSearchNoResultsLabel.textAlignment = .center
        BarsSearchNoResultsLabel.text = "אין תוצאות"
        BarsSearchNoResultsLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 25.0)
        BarsSearchNoResultsLabel.textColor = UIColor.lightGray
        self.view.addSubview(BarsSearchNoResultsLabel)
那么,如何使此代码同时计算
filteredUseres
名称

        if self.names.count == 0
    //Needs to count self.filteredUsers.count == 0 as well ! 
    {
                    self.BarsSearchNoResultsLabel.isHidden = false
                    return 0
                } else {
                    self.BarsSearchNoResultsLabel.isHidden = true
                    return self.names.count;
//Needs to return both names & filteredUsers!
                }
            }
如果你能帮助我,那就太好了

谢谢你的帮助

我尝试的是:


我收到了很多错误。

根据我的说法,最好在tableView中添加backgroundView

重新加载方法后:

tableview.reloadData()
写:

if results.count == 0{
tableview.backroundView = noDataLabel
}
搜索/获取数据时:

tableview.backroundView = nil

根据我的说法,最好在tableView中添加backgroundView

重新加载方法后:

tableview.reloadData()
写:

if results.count == 0{
tableview.backroundView = noDataLabel
}
搜索/获取数据时:

tableview.backroundView = nil
试试这个

@IBOutlet weak var Notfound: UILabel!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var SearchTableView: UITableView!

var searchActive : Bool = false
var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()


    /* Setup delegates */
    SearchTableView.delegate = self
    SearchTableView.dataSource = self
    searchBar.delegate = self
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchText != ""{

        filtered = data.filter({ (text) -> Bool in
            let tmp: NSString = text as NSString
            let range = tmp.range(of: searchText, options: .caseInsensitive)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
            Notfound.isHidden = false
            SearchTableView.isHidden = true
        } else {
            searchActive = true;
            Notfound.isHidden = true
            SearchTableView.isHidden = false
        }
        SearchTableView.reloadData()
    }else{

        filtered = data
        SearchTableView.isHidden = false
        SearchTableView.reloadData()
    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return data.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row];
    }

    return cell;
}
从故事板上取一个UILable放在UIview中

现在就这样用吧

@IBOutlet weak var Notfound: UILabel!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var SearchTableView: UITableView!

var searchActive : Bool = false
var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()


    /* Setup delegates */
    SearchTableView.delegate = self
    SearchTableView.dataSource = self
    searchBar.delegate = self
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchText != ""{

        filtered = data.filter({ (text) -> Bool in
            let tmp: NSString = text as NSString
            let range = tmp.range(of: searchText, options: .caseInsensitive)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
            Notfound.isHidden = false
            SearchTableView.isHidden = true
        } else {
            searchActive = true;
            Notfound.isHidden = true
            SearchTableView.isHidden = false
        }
        SearchTableView.reloadData()
    }else{

        filtered = data
        SearchTableView.isHidden = false
        SearchTableView.reloadData()
    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return data.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row];
    }

    return cell;
}
试试这个

@IBOutlet weak var Notfound: UILabel!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var SearchTableView: UITableView!

var searchActive : Bool = false
var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()


    /* Setup delegates */
    SearchTableView.delegate = self
    SearchTableView.dataSource = self
    searchBar.delegate = self
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchText != ""{

        filtered = data.filter({ (text) -> Bool in
            let tmp: NSString = text as NSString
            let range = tmp.range(of: searchText, options: .caseInsensitive)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
            Notfound.isHidden = false
            SearchTableView.isHidden = true
        } else {
            searchActive = true;
            Notfound.isHidden = true
            SearchTableView.isHidden = false
        }
        SearchTableView.reloadData()
    }else{

        filtered = data
        SearchTableView.isHidden = false
        SearchTableView.reloadData()
    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return data.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row];
    }

    return cell;
}
从故事板上取一个UILable放在UIview中

现在就这样用吧

@IBOutlet weak var Notfound: UILabel!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var SearchTableView: UITableView!

var searchActive : Bool = false
var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()


    /* Setup delegates */
    SearchTableView.delegate = self
    SearchTableView.dataSource = self
    searchBar.delegate = self
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchText != ""{

        filtered = data.filter({ (text) -> Bool in
            let tmp: NSString = text as NSString
            let range = tmp.range(of: searchText, options: .caseInsensitive)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
            Notfound.isHidden = false
            SearchTableView.isHidden = true
        } else {
            searchActive = true;
            Notfound.isHidden = true
            SearchTableView.isHidden = false
        }
        SearchTableView.reloadData()
    }else{

        filtered = data
        SearchTableView.isHidden = false
        SearchTableView.reloadData()
    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return data.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row];
    }

    return cell;
}

从架构的角度来看,我建议您在更新数据源阵列的位置显示/隐藏标签;这样,只有在知道数据已更改时,才能执行代码并显示/隐藏标签。代码的执行也是更新数据的直接结果,而不是重新加载tableview的副作用

但是,如果您想在
numberOfRowsInSection
中执行此操作,您可以使用以下内容:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let namesCount = self.names?.count ?? 0
        let filteredCount = self.filteredUsers?.count ?? 0
        guard namesCount != 0 && filteredCount != 0 else {
            self.BarsSearchNoResultsLabel.isHidden = false
            return 0
        }

        self.BarsSearchNoResultsLabel.isHidden = true
        if tableView == self.tableView {
            return self.names.count
        } else {
            return self.filteredUsers.count
        }
}

从架构的角度来看,我建议您在更新数据源阵列的位置显示/隐藏标签;这样,只有在知道数据已更改时,才能执行代码并显示/隐藏标签。代码的执行也是更新数据的直接结果,而不是重新加载tableview的副作用

但是,如果您想在
numberOfRowsInSection
中执行此操作,您可以使用以下内容:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let namesCount = self.names?.count ?? 0
        let filteredCount = self.filteredUsers?.count ?? 0
        guard namesCount != 0 && filteredCount != 0 else {
            self.BarsSearchNoResultsLabel.isHidden = false
            return 0
        }

        self.BarsSearchNoResultsLabel.isHidden = true
        if tableView == self.tableView {
            return self.names.count
        } else {
            return self.filteredUsers.count
        }
}


只需将
和&self.filteredUsers.count==0
添加到if语句中作为旁注,假设
BarsSearchNoResultsLabel
不是一个类而是一个实例,它应该以小写的
BarsSearchNoResultsLabel
开头。这有关系吗@bauerMusic@NewbieQuestions到编译器?绝对不是,但我们(人类)希望能够理解你的意思;)@Paulw11我如何处理
返回
s我的应用程序崩溃。请参阅问题中的“我尝试了什么”只需将
和&self.filteredUsers.count==0
添加到if语句作为旁注,假设
BarsSearchNoResultsLabel
不是类而是实例,它应该以小写的
BarsSearchNoResultsLabel
开头。这有关系吗@bauerMusic@NewbieQuestions到编译器?绝对不是,但我们(人类)希望能够理解你的意思;)@Paulw11我如何处理
返回
s我的应用程序崩溃。参见“我试过的”在这个问题上,如果我可以说,我更喜欢我的方式…如果你能帮我做我试过的工作,我更喜欢我的方式如果我可以说…我更喜欢你能帮我做我试过的工作,我只是告诉你如何搜索文本,它工作正常,试着测试一下,好的,我现在就试一下。你能不能同时试一下让我试过的东西起作用?我很好奇能不能找到答案。我只是给出了如何搜索文本的方法,效果很好。试着测试一下。我现在就试试。你能不能同时试着让我试过的东西起作用?我很好奇,看看是否可以解决。更新的问题,看,我有7个错误在同一行有一个空间丢失应用程序崩溃前启动。在第行:
guard self.names.count!=0&&self.filteredUsers.count!=0其他{
致命错误:在展开可选值时意外发现nil一个或两个数组必须是隐式展开的可选值-您尚未显示。我已更新代码以检查nil,或者您可以为隐式展开的可选值初始化一个空数组。您可以看到此代码有多凌乱,它是如何显示的在错误的位置。更新的问题,看,我在同一行上有7个错误缺少空格应用程序在启动前崩溃。在第行:
guard self.names.count!=0&&self.filteredUsers.count!=0其他{
致命错误:在展开可选值时意外发现nil一个或两个数组必须是隐式展开的可选值-您尚未显示。我已更新代码以检查nil,或者您可以为隐式展开的可选值初始化一个空数组。您可以看到此代码有多凌乱,它是如何显示的在错误的地方。