ios 使用标题数组更改 UITableView 中的部分标题背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30240594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:58:51  来源:igfitidea点击:

Change the sections header background color in UITableView using an array of headers

iosuitableviewswiftios8

提问by Jp4Real

I have a array of headers that I use

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

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

and they are showed using

它们显示使用

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

Now all of this works fine but I would like to modify the background color of the headers so that they are more visible (Darker Color)

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

any idea if I can do this in a simple line or do I need to use a custom cell to create this

任何想法我是否可以在一条简单的行中做到这一点,或者我是否需要使用自定义单元格来创建它

thanks

谢谢

update

更新

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

采纳答案by Hyman C

Instead of using the

而不是使用

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

data source method, you can use the

数据源方法,您可以使用

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

delegate method and simply customize the UIViewreturned as you wish.

委托方法并根据需要简单地自定义UIView返回的值。

For example set the text of the UILabeltextLabelto your desired value and the backgroundColorto the desired UIColor.

例如,将 的文本设置UILabeltextLabel为您想要的值,并将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

快速 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
        }

回答by Jovan Stankovic

If you're using only titleForHeaderInSection :

如果您只使用 titleForHeaderInSection :

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

回答by AMAN77

You have to keep both

你必须同时保留

titleForHeaderInSection

titleForHeaderInSection

AND

viewForHeaderInSection

viewForHeaderInSection

Here is some working code in Swift 3

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

回答by MAGiGO

SWIFT 4

快速 4

Super easy, by setting header's view, contentView background color..

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

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

回答by Ashu

Swift 4.X

斯威夫特 4.X

This is tested and working 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)
}

回答by Adriatik Gashi

Swift 5iOS 13:

斯威夫特5 iOS 13

//Remove 'override' if you don't override from UITableviewController
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let headerView = view as? UITableViewHeaderFooterView else { return }
    headerView.tintColor = .clear //use any color you want here .red, .black etc
}

回答by rbaldwin

Swift 5

斯威夫特 5

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

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

    headerView.backgroundView?.backgroundColor = .red
}

And, if you want different header background (and/or text) colors for each section:

而且,如果您想要每个部分的标题背景(和/或文本)颜色不同:

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
    }

回答by blacker

Swift 4change background color and text color:

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
}

回答by Gefilte Fish

If you are using titleForHeaderInSection, then the easiest way is to add in viewDidLoad():

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

self.tableView.backgroundColor = UIColor.clear

For some reason, setting ClearColor for Background in the View section for the TableView in the Interface Builder doesnt always set it to transparent. But adding this one line in the code (for me at least) does.

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

回答by Dasoga

Swift 3+

斯威夫特 3+

I used this:

我用过这个:

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

}