Changer la couleur de fond de l'en-tête des sections dans UITableView en utilisant un tableau d'en-têtes
j'ai un tableau d'en-têtes que j'utilise
let sectionHeaderTitleArray = ["test1","test2","test3]
et ils sont montré à l'aide de
func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
} 
maintenant tout cela fonctionne très bien mais je voudrais modifier la couleur de fond des en-têtes afin qu'ils soient plus visibles (couleur plus foncée)
aucune idée si je peux le faire en une ligne simple ou dois-je utiliser une cellule personnalisé pour créer cette
merci
mise à jour
  //number of sections and names for the table
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.sectionHeaderTitleArray.count
    }
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sectionHeaderTitleArray[section] as String
    }
    func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
        return self.tableView.backgroundColor = UIColor.lightGrayColor()
    }
9 réponses
au Lieu d'utiliser le
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String?
source de données, vous pouvez utiliser le
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
 méthode de délégation et simplement personnaliser le UIView retourné comme vous le souhaitez.
 par exemple, définissez le texte du UILabeltextLabel à votre valeur désirée et le backgroundColor pour le désiré UIColor.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let returnedView = UIView(frame: CGRectMake(x, y, width, height)) //set these values as necessary
    returnedView.backgroundColor = UIColor.lightGrayColor()
    let label = UILabel(frame: CGRectMake(labelX, labelY, labelWidth, labelHeight))
    label.text = self.sectionHeaderTitleArray[section]
    returnedView.addSubview(label)
    return returnedView
}
si vous utilisez uniquement titleForHeaderInSection:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}
vous devez garder les deux
titre forheaderinsection
et
view forheaderinsection
voici un peu de code de travail dans Swift 3
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sectionHeaderTitleArray[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
    returnedView.backgroundColor = .lightGray
    let label = UILabel(frame: CGRect(x: 10, y: 7, width: view.frame.size.width, height: 25))
    label.text = self.sectionHeaderTitleArray[section]
    label.textColor = .black
    returnedView.addSubview(label)
    return returnedView
}
SWIFT 4
Super facile, en définissant la vue de l'en-tête, la couleur de fond de contentView..
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
   let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
   header.contentView.backgroundColor = AnyColor
   return header
}
Swift 3+
j'ai utilisé ceci:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 20))
    headerView.backgroundColor = .lightGray
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = stringValues[section]
    headerView.addSubview(label)
    label.leftAnchor.constraint(equalTo: headerView.leftAnchor).isActive = true
    label.rightAnchor.constraint(equalTo: headerView.rightAnchor).isActive = true
    label.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
    label.heightAnchor.constraint(equalToConstant: 25).isActive = true
    return headerView
}
Si vous utilisez titleForHeaderInSection, alors la façon la plus simple est d'ajouter dans viewDidLoad ():
self.tableView.backgroundColor = UIColor.clear
pour une raison quelconque, définir ClearColor pour Background dans la section View pour TableView dans le constructeur de L'Interface ne le définit pas toujours à transparent. Mais en ajoutant cette ligne dans le code (pour moi au moins).
solution Swift 4.
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView:UIView = UIView()
        headerView.backgroundColor = UIColor.lightGray
   return headerView
}
Cela va changer pour tous les en-têtes dans votre application :
UITableViewHeaderFooterView.appearance().backgroundColor = theme.subViewBackgroundColor
Swift 4.X
ceci est testé et fonctionne en code.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Total Count (41)"
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tintColor = UIColor.lightGray
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.darkGray
    header.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}