Question

I'm having some trouble changing the colour and font on a TableView that I have split into 4 sections with names etc., I can;t seem to get it to work I'm not sure what I am doing wrong?

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

NSString *sectionName = nil;

switch(section)
{
    case 0:
        sectionName = [NSString stringWithString:@"Date"];
        break;
    case 1:
        sectionName = [NSString stringWithString:@"Gig"];
        break;
    case 2:
        sectionName = [NSString stringWithString:@"City"];
        break;
    case 3:
        sectionName = [NSString stringWithString:@"Country"];
        break;
}

UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]   autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;

return sectionName;
}
Was it helpful?

Solution

you should return the view instead of the string...

This is what you are returning

return sectionName;

And this is what you should return..

return sectionHeader;

OTHER TIPS

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

is the right method for presenting a view in header of UITableView. Here you can return your UILabel object.

You are trying to return a NSString object instead of UILabel. Also the return type of method is wrong. It should be of the type UIView.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

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

NSString *sectionName = nil;
switch(section)
{
    case 0:
    sectionName = [NSString stringWithString:@"Date"];
    break;
case 1:
    sectionName = [NSString stringWithString:@"Gig"];
    break;
case 2:
    sectionName = [NSString stringWithString:@"City"];
    break;
case 3:
    sectionName = [NSString stringWithString:@"Country"];
    break;
}

UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]   autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionHeader;

}

The code shows you how to implement viewForHeaderInSection delegate method of TableViewCOntroller which you need to implement to accomplish what you need. your code returns NSString, which is supposed to return UIView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top