sectionName TableView - 我做错了什么?

时间:2022-09-20 19:01:29

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?

我在更改TableView上的颜色和字体方面遇到了一些麻烦,我将其分为4个部分,其中包含名称等等,我可以;似乎可以让它工作我不知道我做错了什么?

- (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;
}

3 个解决方案

#1


4  

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;

#2


2  

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

是在UITableView的标题中显示视图的正确方法。在这里,您可以返回您的UILabel对象。

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.

您正在尝试返回NSString对象而不是UILabel。返回类型的方法也是错误的。它应该是UIView类型。

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

#3


1  

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

该代码向您展示了如何实现TableViewCOntroller的viewForHeaderInSection委托方法,您需要实现该方法以实现所需。你的代码返回NSString,它应该返回UIView。

#1


4  

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;

#2


2  

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

是在UITableView的标题中显示视图的正确方法。在这里,您可以返回您的UILabel对象。

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.

您正在尝试返回NSString对象而不是UILabel。返回类型的方法也是错误的。它应该是UIView类型。

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

#3


1  

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

该代码向您展示了如何实现TableViewCOntroller的viewForHeaderInSection委托方法,您需要实现该方法以实现所需。你的代码返回NSString,它应该返回UIView。