没有结果时隐藏海关部分?

Hide customs section when no result?

我有两个部分header第一部分是评估列表,第二部分是公司列表。当过滤器数组的结果为空时,我试图隐藏 header 部分。如果你看到这张照片,我有一个错误,其中显示了 headers 部分。如何修复我的代码以及如何组织它?

class HubTableViewController: UITableViewController, UISearchBarDelegate {

    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        var numberOfsections = 0
        if isSearching {
            numberOfsections = 2
            
        }
        else {
            numberOfsections = 2
        }
        return numberOfsections
    }

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.isSearching ){
            
            switch Device.size() {
            case .screen4_7Inch:
                if section == 0{
                    return  filteredAssessments!.count
                }
                else {
                    return filteredCompanies!.count
                }

            default:
                if section == 0{
                    return  filteredAssessments!.count
                }
                else {
                    return filteredCompanies!.count
                }
            }
        }
        else {
            switch Device.size() {
            case .screen4_7Inch:
                if section == 0{
                    return 3
                }
                else {
                    return 3
                }

            default:
                if section == 0{
                    return 3//    return  filteredAssessments!.count
                }
                else {
                    return 3//  return filteredCompanies!.count
                }
            }
            
        }
    }
    
    func addDoneButtonOnKeyboard() {
        
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        doneToolbar.barStyle = .default
        
        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
        
        let items = [flexSpace, done]
        doneToolbar.items = items
        doneToolbar.sizeToFit()
        
        searchBar.inputAccessoryView = doneToolbar
    }
    
    @objc func doneButtonAction(){
        noResultView.removeFromSuperview()
        self.searchBar.resignFirstResponder()
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
        if isSearching {
            if indexPath.section == 0{
                let cell = tableView.dequeueReusableCell(withIdentifier: "assessmentCell", for: indexPath) as! AssessmentsTableViewCell
                if let a = filteredAssessments?[indexPath.row]{
                    cell.setlblAssessmentName(a.assessmentName)
                    cell.setlblAssessmentsType(a.assessmenType)
                    cell.setlblBalance(a.balance)
                }
                return cell
            }else {
                let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell", for: indexPath) as! CompanyTableViewCell
                if let c = filteredCompanies?[indexPath.row]{
                    cell.setLblCompanyName(c.companyName)
                    cell.setLblNumberOfUsers(c.numberofUser)
                }
                return cell
            }
        }
        else {
            
            if indexPath.section == 0{
                let cell = tableView.dequeueReusableCell(withIdentifier: "assessmentCell", for: indexPath) as! AssessmentsTableViewCell
                if let a = assessments?[indexPath.row]{
                    cell.setlblAssessmentName(a.assessmentName)
                    cell.setlblAssessmentsType(a.assessmenType)
                    cell.setlblBalance(a.balance)
                }
                return cell
            }else {
                let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell", for: indexPath) as! CompanyTableViewCell
                if let c = companies?[indexPath.row]{
                    cell.setLblCompanyName(c.companyName)
                    cell.setLblNumberOfUsers(c.numberofUser)
                }
                return cell
            }
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

            if (section == 0){
                
                let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:
                    "sectionHeader") as! HubCustomHeader
                view.title.text = "My Balances"
                view.button.setTitle("VIEW ALL", for: .normal)
                
                return view
                
            }
            else {
                let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:
                    "sectionHeader") as! HubCustomHeader
                view.title.text = "My Customers"
                view.button.setTitle("ADD", for: .normal)
                return view
            }
        
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section ==  0{
            return 50
        }
        else {
            
            return 50
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == "" {
            isSearching = false
            noResultView.removeFromSuperview()
            hubtableView.reloadData()
        } else {
            isSearching = true
            filteredAssessments = self.assessments?.filter {
                [=10=].assessmentName.range(of: searchText, options: .caseInsensitive)  != nil
            }
           
            filteredCompanies = self.companies?.filter {
                [=10=].companyName.range(of: searchText, options: .caseInsensitive)  != nil
            }
         hubtableView.reloadData()
            if(filteredCompanies!.isEmpty   ||  filteredCompanies!.isEmpty ){
                view.backgroundColor = UIColor.white
                view.addSubview(noResultView)
                noResultView.center = view.center
                self.hubtableView.reloadData()
                hubtableView.separatorStyle = .none
            }
            else
            {
                tableView.reloadData()
            }
        }
    }
  
}

如果你没有结果显示然后设置部分'0'

    override func numberOfSections(in tableView: UITableView) -> Int {
        var numberOfsections = 0
        if !result.isEmpty  {
            if isSearching {
                numberOfsections = 2
            } else {
                numberOfsections = 2
            }
        }
        return numberOfsections
     }

当您试图隐藏 header 时,将 heightForHeaderInSection 方法更新为 return 高度 0。

var hideHeader = false // toggle this and reload when you want to hide the header.

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    hideHeader ? 0 : 50
}