Viva
Viva

Reputation: 79

Section header background color changes when selected in swift

I have a table view with with multiple sections. I have two colors. White and blue. If a section satisfies the requirement it should be set to blue background permanently. I have an issue. I am able to set the background color of the section header but the blue color is only visible when the section header is selected. I would like to see all section headers that satisfy the requirement are set to blue and the others white. I have tried searching out for answers but i can't see any question of this type.

Here is my code

 //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)
    }

Below is where i change the header background color

    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, y: 0, 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, y: 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
    }
}

Upvotes: 0

Views: 244

Answers (1)

PGDev
PGDev

Reputation: 24341

Try this,

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
}

Upvotes: 1

Related Questions