문제

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;
}
도움이 되었습니까?

해결책

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;

다른 팁

- (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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top