Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UITableView Section color and Text color

I have UITableView which populates dates from the plist and Even Section's Title is been populated from plist in UITableView.it gives me the default blue section color and White text color.like this...

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


NSString *key = nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
    key = [self.searchResults objectAtIndex:section];
}
else{
    key = [self.mySections objectAtIndex:section];
}

//    NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];

 }

. Now I need to change this default text color and Section's color, to do that I am implementing code shown below.But it gives me its own UIView.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}
like image 507
Christien Avatar asked Dec 22 '12 08:12

Christien


People also ask

How do I change the background color of a header in Swift?

you need to set it with returnedView. backgroundColor = UIColor. lightGrayColor() where returnedView is the UIView you initialize in func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?


1 Answers

To best customize the look of a table section header, you really need to implement two methods: the first one you already have, and it should work, albeit the results won't be very usable.

The second method is the tableView:heightForHeaderInSection:, which tells the UITableView what's the height of the new section, and it can be as simple as this:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0f; 
}

EDIT: As per the comment, here's the result of your code and defining the header height:

final header label look

EDIT 2: If you want red text with a black background, change your code for tableView:viewForHeaderInSection: like this:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor blackColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

EDIT 3: Ok, so I'll try to merge your code from the first method with the second one. It will look like this:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor blackColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];

    NSString *key = nil;
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
    {
        key = [self.searchResults objectAtIndex:section];
    }
    else{
        key = [self.mySections objectAtIndex:section];
    }

    tempLabel.text=[NSString stringWithFormat:@"%@", key];
    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

This should return a table view section header with the proper label and proper look.

EDIT 4: Just a note on how all of this works: if you use tableView:viewForHeaderInSection: whatever code you put in tableView:titleForHeaderInSection: will be ignored. So you need to do the whole setup of the section header, including the proper text in the tableView:viewForHeaderInSection method.

like image 127
nikolovski Avatar answered Sep 29 '22 00:09

nikolovski