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

标签 ios swift uitableview searchbar

我有两个部分的标题,第一部分是评估列表,第二部分是公司列表。当过滤器数组的结果为空时,我试图隐藏节标题。如果您看到照片,我有一个显示部分标题的错误。如何修复我的代码以及如何组织它? 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()
            }
        }
    }
  
}

最佳答案

更新 heightForHeaderInSection当您尝试隐藏标题时返回高度 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
}

关于ios - 没有结果时隐藏海关部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62718881/

相关文章:

ios - 在启用自动布局的情况下在 swift/xcode 中调整按钮大小

ios - UITableView 在开始/结束更新调用调整高度后自动滚动到顶部

ios - 是否可以设置自定义单元格的 textLabel?

ios - 如何管理包含重复依赖项的依赖树? (X代码,iOS)

ios - 我今天的扩展程序的主体没有显示 swift

ios - 如何从iPhone到Mac提取IPA文件?

swift - 如何分配播放按钮以仅播放解析中的第一个对象?

iphone - 使用 CABasicAnimation 淡入显示图像

ios - 从 Swift 调用时,AWS Cognito 不会创建 Twitter/数字登录

ios - 如何动态设置tableview单元格高度?