Changing color of section header in UITableview
Asked Answered
M

3

10

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.

Morven answered 5/3, 2010 at 21:1 Comment(1)
Here is a similar question: #813568Lacteous
P
18

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.

Plentiful answered 5/3, 2010 at 22:0 Comment(3)
Thanks, i thought there would some kind of trigger in IB to switch to black color. But anyway, thanks.Morven
But please, cache those views. There is bug in UIKit causing to ask for header views when the tableView is being scrolled, so this method will get called for every pixel-offset while scrolling. 22px is indeed the default height.Untwist
hi, i understand this method gets called when scrolling takes place... how can we cache this so to save unnecessary views being created etc..? if you could post a comment or an update to your post it will be very helpful. thank youRoemer
T
21

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/

Tympanum answered 4/10, 2013 at 5:11 Comment(0)
P
18

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.

Plentiful answered 5/3, 2010 at 22:0 Comment(3)
Thanks, i thought there would some kind of trigger in IB to switch to black color. But anyway, thanks.Morven
But please, cache those views. There is bug in UIKit causing to ask for header views when the tableView is being scrolled, so this method will get called for every pixel-offset while scrolling. 22px is indeed the default height.Untwist
hi, i understand this method gets called when scrolling takes place... how can we cache this so to save unnecessary views being created etc..? if you could post a comment or an update to your post it will be very helpful. thank youRoemer
W
0
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
   {
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.bounds.size.width, 30)];
     if (section == 0)
     [headerView setBackgroundColor:[UIColor redColor]];
     else 
     [headerView setBackgroundColor:[UIColor clearColor]];
     return headerView;
   }
Wilbert answered 23/7, 2013 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.