使用标头数组更改UITableView中的节标题背景颜色

问题描述 投票:37回答:12

我有一个我使用的标头数组

let sectionHeaderTitleArray = ["test1","test2","test3]

他们被展示使用

func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
} 

现在所有这一切工作正常,但我想修改标题的背景颜色,使它们更加明显(深色)

任何想法,如果我可以在一个简单的行中这样做或我需要使用自定义单元格来创建它

谢谢

更新

  //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()
    }
ios uitableview swift ios8
12个回答
23
投票

而不是使用

func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String?

数据源方法,你可以使用

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

委托方法,只需根据需要自定义UIView

例如,将UILabel textLabel的文本设置为所需的值,将backgroundColor设置为所需的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
}

SWIFT 5

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let returnedView = UIView(frame: CGRect(x: x, y: y, width: width, height: height)) //set these values as necessary
            returnedView.backgroundColor = .white

            let label = UILabel(frame: CGRect(x: x, y: y, width: width, height: height))

            label.text = self.sectionHeaderTitleArray[section]
            returnedView.addSubview(label)

            return returnedView
        }

0
投票

这将为您的应用程序中的所有标题更改它:

UITableViewHeaderFooterView.appearance().backgroundColor = theme.subViewBackgroundColor

0
投票

覆盖viewForHeaderInSection并创建UITableViewHeaderFooterView

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var headrview = tableView.dequeueReusableHeaderFooterView(withIdentifier: "aaa")
    if headrview == nil {
        headrview = UITableViewHeaderFooterView(reuseIdentifier: "aaa")
    }
    let bview = UIView()
    bview.backgroundColor = Theme.current.navigationColor
    headrview?.backgroundView = bview
    headrview?.textLabel?.textColor = Theme.current.primaryTextColor
    return headrview
}

0
投票

Swift 4改变背景颜色和文字颜色:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let tableView = view as? UITableViewHeaderFooterView else { return }
    tableView.backgroundView?.backgroundColor = UIColor.black
    tableView.textLabel?.textColor = UIColor.white
}

65
投票

如果您只使用titleForHeaderInSection:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}

15
投票

你必须保持两者

titleForHeaderInSection

viewForHeaderInSection

这是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
}

10
投票

SWIFT 4

超级简单,通过设置标题的视图,contentView背景颜色..

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
   let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
   header.contentView.backgroundColor = AnyColor
   return header
}

6
投票

Swift 4.X

这是经过测试和运行的代码。

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

3
投票

Swift 3+

我用过这个:

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

}

2
投票

如果您使用的是titleForHeaderInSection,那么最简单的方法是在viewDidLoad()中添加:

self.tableView.backgroundColor = UIColor.clear

出于某种原因,在Interface Builder的View部分中为InterfaceView设置ClearColor for Background并不总是将其设置为透明。但是在代码中添加这一行(至少对我来说)确实如此。


1
投票

Swift 4解决方案。

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView:UIView = UIView()
        headerView.backgroundColor = UIColor.lightGray
   return headerView
}

1
投票

斯威夫特5

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    guard let headerView = view as? UITableViewHeaderFooterView else { return }

    headerView.backgroundView?.backgroundColor = .red
}

并且,如果您希望每个部分使用不同的标题背景(和/或文本)颜色:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    guard let headerView = view as? UITableViewHeaderFooterView else { return }

    switch section {

    case 0:
        headerView.backgroundView?.backgroundColor = .red
        headerView.textLabel?.textColor = .white
    case 1:
        headerView.backgroundView?.backgroundColor = .green
        headerView.textLabel?.textColor = .white
    case 2:
        headerView.backgroundView?.backgroundColor = .yellow
        headerView.textLabel?.textColor = .black

        // etc

    default:
        return
    }
© www.soinside.com 2019 - 2024. All rights reserved.