Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of section header in UITableview

i have pretty simple simple question (i hope so). How do i change the section header color in a UITableview from default blue to black transparent? Thanks in advance.

like image 788
Simon D. Avatar asked Mar 05 '10 21:03

Simon D.


People also ask

How to change the background and text color of UITableView section?

Set the background and text color of section area: (Thanks to William Jockusch and Dj S) To change the background color, text label color and font for the Header View of a UITableView Section, simply override willDisplayHeaderView for your table view like so:

How to add header and footer in UITableView?

There are two ways that we can add the header and footer, one way is to add it in using Interface Builder and the other way is to do it with code. In this tutorial we will be using both Interface Builder as well as code. The first thing that we need to do is to set up the UITableView and get it to show some data.

How to create a header for a section of a Tableview?

The first way will use set the tableViewHeader property on the tableview and the second way will use UITableViewDelegate methods. The second way is more powerful and it allows you to easily create a header for sections of a tableview. I have added the following code to my viewDidLoad method.

Why does my UITableView height not match my layout height?

The problem is that UITableView does something to the header/footer view, encapsulating it, and creating a required (priority 1000) constraint on the height. And this height will be slightly taller than your layout (maybe to cater for the separator?). Anyway, the height does not match.


2 Answers

you need to implement this method in the UITableViewDelegate protocol:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

Here is a link to the documentation

... and do something like this (sub in your own color):

UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)] autorelease];
[sectionView setBackgroundColor:[UIColor blackColor]];
return sectionView;

You can also use the section integer to alternate colors or something similar. I think the default height for the sections is 22, but you can make it whatever you want. Is this what you meant by your question? Hope this helps.

like image 84
Ryan Ferretti Avatar answered Oct 24 '22 05:10

Ryan Ferretti


This is an old question, but I think the answer needs to be updated.

This method does not involve defining your own custom views.
In iOS 6 and up, you can easily change the background color and the text color by defining the

- (void)tableView:(UITableView *)tableView 
        willDisplayHeaderView:(UIView *)view 
        forSection:(NSInteger)section
delegate method.

For example:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

Taken from my post here: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

like image 35
Dj S Avatar answered Oct 24 '22 04:10

Dj S