Uitableview - Change Section Header Color

UITableView - change section header color

Hopefully this method from the UITableViewDelegate protocol will get you started:

Objective-C:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}

Swift:

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.redColor()
} else {
headerView.backgroundColor = UIColor.clearColor()
}
return headerView
}

Updated 2017:

Swift 3:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.red
} else {
headerView.backgroundColor = UIColor.clear
}
return headerView
}

Replace [UIColor redColor] with whichever UIColor you would like. You may also wish to adjust the dimensions of headerView.

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

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 UIView returned as you wish.

For example set the text of the UILabel textLabel to your desired value and the backgroundColor to the desired 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
}

How to change the section header text color in UITableView

How did you register your header view cell in Section of TableView?? Please show more code

I recommend that you should implement your table header view cell in Section like the following steps:

Step 1: Create YourTableHeaderViewCell

iOS Sample Image 37

Step 2: Add text label in YourTableHeaderViewCell.xib

iOS Sample Image 38

Step 3: Create the outlet text label in YourTableHeaderViewCell.swift

iOS Sample Image 39

It became like:

class YourTableHeaderViewCell: UITableViewCell {

// MARK: - Outlets
@IBOutle weak var textLabel: UILabel!

// MARK: - Variables
static var identifier = "YourTableHeaderViewCell"
}

Step 4: Setup YourViewController and Register your table header view cell:

Sample Image

class YourViewController: UIViewController {

// MARK: - View Cycle
override func viewDidLoad() {
super.viewDidLoad()

setupTableView()
}

private func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.clear
tableView.register(UINib.init(nibName: YourTableViewHeaderView.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewHeaderView.identifier) // register your table header view cell
tableView.register(UINib.init(nibName: YourTableViewCell.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewCel.identifier) // register your table view cell
}
}

// MARK: - UITableViewDataSource
extension YourViewController: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
return // number of your sections
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return // number of rows in sections
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewCell.identifier, for: indexPath) as? YourTableViewCell {
return cell
}
return UITableViewCell()
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewHeaderView.identifier) as? YourTableViewHeaderView {
cell?.textLabel?.textColor = UIColor.white // Change the text color here
return cell.contentView
}
return UIView()
}
}

// MARK: - UITableViewDelegate
extension YourViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}
}

Updating the background colour of UITableView section header

The proper solution for your task is to use a notification. When the user updates a setting, use NotificationCenter to post a notification such as "color scheme changed".

Then any class that needs to perform an action when this notification is posted can register to receive this specific notification.

You can have this view controller listen for the notification and reload the table view as needed. Any other views, controls, or controllers that also need to update themselves based on the notification can register as well and update themselves as needed.

This is a much better solution than relying on other events such as a view becoming visible again. It also eliminates needlessly reloading a table every time the view controller is viewed even if the user didn't change any setting.

How do you change the colour of a section title in a tableview?

you can use the one of UITableViewDelegate's method

swift3 and above

  func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.contentView.backgroundColor = .white
headerView.backgroundView?.backgroundColor = .black
headerView.textLabel?.textColor = .red
}
}

objective C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView * headerView = (UITableViewHeaderFooterView *) view;
headerView.textLabel.textColor = [UIColor RedColor];
}
}

for Reference I taken the model answer from here

UITableView change header title color

You can try following line of code in table cell creation method -

cell.textLabel.backgroundColor = //Your color;
cell.detailTextLabel.backgroundColor = //Your color;

You can refer following for more detail description, where you can find detail example of creating custom sectioned table similar to what you have mentioned -
http://undefinedvalue.com/2009/08/25/changing-background-color-and-section-header-text-color-grouped-style-uitableview

Changing the color of UITableView section headers

This is something I spent quite a while grappling with myself, only to find that there is no way to just change the tintColor of the section header. The solution I came up with was to screenshot the background of the section header, change the tint of it in Photoshop, and then use that as the background of the section header. Then its just a case of laying out the label.

As you said, the thing to use is the viewForHeaderInSection delegate method.

Here is what I've found works and looks like the Apple default:

UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 320.0, 22.0)] autorelease];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"headerbackground.png"]];;

UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
headerLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
headerLabel.frame = CGRectMake(11,-11, 320.0, 44.0);
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.text = @"Header Label";
[customView addSubview:headerLabel];
return customView;

Here "headerbackground.png" is a 1 pixel by 22 pixel (double that for iPhone 4) image that will get repeated across the length of the header.

Hope this helps!

How to set background colour for header in TableView?

Use viewForHeaderInSection method of UItableview and Create UIview

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}


Related Topics



Leave a reply



Submit