AlexanderDeep
AlexanderDeep

Reputation: 205

changing the color of titleFor Header in section

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

{

}

Hi
I am very new in objective c.......................through this method we can get the title for header section .but how to change the color of that string?can I do that........if any one knows that please tell me.

Regards

Upvotes: 4

Views: 3569

Answers (4)

niceamitsingh
niceamitsingh

Reputation: 41

Please use following code & change the color of your UITableView header

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,44)];
    tempHeaderView.backgroundColor=[UIColor clearColor];
    UILabel *tempHeaderLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,320,44)];
    tempHeaderLabel.backgroundColor=[UIColor clearColor];
    tempHeaderLabel.text=@"HEADER";
    [tempHeaderView addSubView: tempHeaderLabel];
    return tempHeaderView;
}

Upvotes: 1

Steve Weller
Steve Weller

Reputation: 4619

Beware that the view you return is not retained by the UITableView (OS 3.1.2 at least seems to display this problem). This leads to hard-to-find crashes that occur before execution gets to viewDidLoad.

The table view doesn't grab your views on demand as you would think. It requests them all, then requests them all again, and sometimes several more times, so generating them on every request is very inefficient.

Upvotes: 2

Pavel Yakimenko
Pavel Yakimenko

Reputation: 3254

You may try folowing: Header with two customized labels

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
                                                      (NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
    tableView.bounds.size.width, 22)];

    NSString *dateStr = [[self.data allKeys] objectAtIndex:section];
    CGFloat labelWidth = tableView.bounds.size.width / 2;
    CGFloat padding = 5.0;

    UILabel *labelOne = [[UILabel alloc] initWithFrame:CGRectMake
                        (padding, 0, (labelWidth - padding), 22)];
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textAlignment = UITextAlignmentLeft;
    labelOne.text = dateStr;

    UILabel *labelTwo = [[UILabel alloc] initWithFrame:CGRectMake
    (labelWidth, 0, (labelWidth - padding), 22)];
    labelTwo.backgroundColor = [UIColor clearColor];
    labelTwo.textAlignment = UITextAlignmentRight;
    labelTwo.text = @"This is Label TWO";

    [headerView addSubview:labelOne];
    [headerView addSubview:labelTwo];

    [labelOne release];
    [labelTwo release];

    return headerView;
}

Upvotes: 5

Dave DeLong
Dave DeLong

Reputation: 243146

Use tableView:viewForHeaderInSection: instead. That way you can configure a UILabel or whatever to your like (complete with font, transparency, color, etc), and the tableview will use that view as the header as opposed to the default view.

Upvotes: 2

Related Questions