Change UITable section backgroundColor without loosing section Title
Asked Answered
I

5

7

i have changed the backgroundColor/backgroundImage of my tableviews sections.
I am using plain tableView. No worries, get worked without problems with this code:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//For using an image use this:
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

return sectionView;
}  

Problem now is, the section title is gone.

I am setting the title of the section with this code:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    return @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}


}

i am getting the section titles if i do not use

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

Need your help.
Thanks a lot.

EDIT 1:

Thanks to Mutix:

in my case, the answer would be:

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




UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)];
titleLabel.backgroundColor = [UIColor clearColor];

//section Title
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    titleLabel.text = @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    titleLabel.text = [sectionInfo name];
}



[sectionView addSubview:titleLabel];
[titleLabel release];

return sectionView;

}

Inotropic answered 7/12, 2011 at 9:59 Comment(0)
E
6

The viewForHeaderInSection overrides the titleForHeaderInSection method.

To add a title to your header when using viewForHeaderInSection you will need to add a label to the section header view like so:

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

    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];

    sectionView.backgroundColor = [UIColor greenColor];

    //Add label to view
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
    titleLabel.text = @"title";
    [sectionView addSubview:titleLabel];
    [titleLabel release];

    return sectionView;
}  
Elwandaelwee answered 7/12, 2011 at 10:6 Comment(2)
From iOS onwards we can use - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section for this purpose. I have explained my answer below.Sequoia
Clarification: that's from iOS6 onwardsReceptor
S
13

We have a better solution for this from iOS6 onwards:

There is a delegate method

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

You can use this method like this

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

     //Set the background color of the View
     view.tintColor = [UIColor blueColor];

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

}
Sequoia answered 28/2, 2014 at 7:39 Comment(0)
E
6

The viewForHeaderInSection overrides the titleForHeaderInSection method.

To add a title to your header when using viewForHeaderInSection you will need to add a label to the section header view like so:

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

    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];

    sectionView.backgroundColor = [UIColor greenColor];

    //Add label to view
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
    titleLabel.text = @"title";
    [sectionView addSubview:titleLabel];
    [titleLabel release];

    return sectionView;
}  
Elwandaelwee answered 7/12, 2011 at 10:6 Comment(2)
From iOS onwards we can use - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section for this purpose. I have explained my answer below.Sequoia
Clarification: that's from iOS6 onwardsReceptor
R
2

Implement method below from UITableViewDelegate, and needed ui view.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView()
    view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
    return view
}

If you need change footer use similar code in ...viewForFooterInSection.. method

Want to draw you own font, use similar customization:

...    
let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
label.font = UIFont(name: "HelveticaNeue", size: 14)!
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.contentView.addSubview(label)

view.textLabel.hidden = true
...
Rumple answered 13/3, 2015 at 22:43 Comment(0)
S
1

Just add [titleLabel setBackgroundColor:[UIColor clearColor]];
because in my case the label overlapped the green color.

Scoreboard answered 12/2, 2013 at 13:47 Comment(0)
S
0

~Best Solution iPhone 3.5 and 4 screen~

Set the back color or background image from this code. Here you don't have to calculate for section height, just set in xib.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)];
    sectionView.backgroundColor = [UIColor greenColor];
    
    return sectionView;
}
Semite answered 14/9, 2013 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.