快速选择节标题背景颜色

我有一个包含多个部分的表格视图。我有两种颜色。白色和蓝色。如果某个部分满足要求,则应将其永久设置为蓝色背景。我有一个问题。我可以设置节标题的背景色,但是蓝色只有在选择节标题时才可见。我希望看到满足要求的所有节标题都设置为蓝色,其他标题设置为白色。我尝试搜索答案,但看不到任何此类问题。

这是我的代码

 //i put all the index of the sections that satisfy this requirement in an array
 if(objModel.getPaidStatus(index:indexPath.section,subIndex: indexPath.row) == "paid") || (objTeacherVewModel.getPaidStatus(index:indexPath.section,subIndex: indexPath.row) == "firstFree"){
           cell.btnDelete.isHidden = true
            cell.containerVew.backgroundColor = #colorLiteral(red: 0.4935901165,green: 0.7977404594,blue: 0.860445559,alpha: 1)
         
            bookedSection =  indexPath.section
            
            bookedArr.append(indexPath.section)
            print(bookedArr)
    }

下面是我更改标题背景颜色的地方

    func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
    if(tableView == hrsTblVew){
        return nil
    }
    else{
      
    let headerView = UIView(frame: CGRect(x: 0,y: 0,width: tableView.frame.width-40,height: 40))
    
    let imgView = UIImageView(frame: CGRect(x: 10,y: 10,width: 20,height: 20))
 
    if(selectedSection == section){
        imgView.image = #imageLiteral(resourceName: "uparrow")
    }else{
        imgView.image = #imageLiteral(resourceName: "dropdown")
    }
    headerView.addSubview(imgView)
        print(bookedSection)
        
        for item in bookedArr{
        if( section == item){
                           
            headerView.backgroundColor = #colorLiteral(red: 0.2392156869,green: 0.6745098233,blue: 0.9686274529,alpha: 1)
        
            
            
                       }
            else{
                           
            headerView.backgroundColor = #colorLiteral(red: 1,green: 1,blue: 1,alpha: 1)
            }
            
        }
    
    let lbl = UILabel(frame: CGRect(x: 40,width: tableView.frame.width-50,height: 40))
    
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    if let date = dateFormatter.date(from: objModel.getDateString(index: section)){
        
        
        dateFormatter.dateFormat = "EEEE,dd-MM-yyyy"
        
        let dayStr = dateFormatter.string(from: date)
        lbl.text = Utility.shared().getcurrentDay(str:dayStr,currentLang:UserDefaults.standard.object(forKey:"language")as?String ?? "pl-PL")
        
    }
    
    
    lbl.textColor = UIColor.darkGray
    lbl.font = UIFont(name:"Montserrat-SemiBold",size: 16)
    headerView.addSubview(lbl)
    
    
    let btn = UIButton(frame: CGRect(x: 0,width: tableView.frame.width,height: 40))
    btn.addTarget(self,action: #selector(headerTapped(sender:)),for: .touchUpInside)
    btn.tag = section + 1
    headerView.addSubview(btn)
        
        
        
    
    return headerView
    }
}
iCMS 回答:快速选择节标题背景颜色

尝试一下,

func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
    //rest of the code...
    if bookedArr.contains(section) {
        headerView.backgroundColor = #colorLiteral(red: 0.2392156869,green: 0.6745098233,blue: 0.9686274529,alpha: 1)
    } else {
        headerView.backgroundColor = #colorLiteral(red: 1,green: 1,blue: 1,alpha: 1)
    }
    //rest of the code...
    return headerView
}
本文链接:https://www.f2er.com/1940736.html

大家都在问