Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 435
AlexanderDeep Avatar asked Dec 30 '22 14:12

AlexanderDeep


2 Answers

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;
}
like image 115
Pavel Yakimenko Avatar answered Jan 11 '23 00:01

Pavel Yakimenko


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.

like image 36
Steve Weller Avatar answered Jan 10 '23 23:01

Steve Weller