Hide customs section when no result

I have two sections header the first section is the assessments list and the second section companies list. I am trying to hide the section header when the result of the filter arrays are empty. if you see the photo I have a bug where sections headers are shown. how to fix my code and how to organize it?enter image description here

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 {
                $0.assessmentName.range(of: searchText, options: .caseInsensitive)  != nil
            }
           
            filteredCompanies = self.companies?.filter {
                $0.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()
            }
        }
    }
  
}

Go to Source
Author: developer16