xcode 如何更改 UITableView 部分颜色和文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14001194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change UITableView Section color and Text color
提问by Christien
I have UITableView
which populates dates from the plist and Even Section's Title is been populated from plist in UITableView
.it gives me the default blue
section color and White
text color.like this...
我有UITableView
从 plist 中填充日期,甚至部分的标题是从 plist 中填充的。UITableView
它给了我默认的blue
部分颜色和White
文本颜色。像这样......
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
key = [self.searchResults objectAtIndex:section];
}
else{
key = [self.mySections objectAtIndex:section];
}
// NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];
}
.
Now I need to change this default text color and Section's color, to do that I am implementing code shown below.But it gives me its own UIView
.
. 现在我需要更改此默认文本颜色和部分的颜色,为此我正在实现如下所示的代码。但它给了我自己的UIView
.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
tempView.backgroundColor=[UIColor clearColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
tempLabel.text=@"Header Text";
[tempView addSubview:tempLabel];
[tempLabel release];
return tempView;
}
回答by nikolovski
To best customize the look of a table section header, you really need to implement two methods: the first one you already have, and it should work, albeit the results won't be very usable.
为了最好地自定义表格部分标题的外观,您确实需要实现两种方法:第一种方法是您已经拥有的,它应该可以工作,尽管结果不会很有用。
The second method is the tableView:heightForHeaderInSection:
, which tells the UITableView
what's the height of the new section, and it can be as simple as this:
第二种方法是tableView:heightForHeaderInSection:
,它告诉UITableView
新部分的高度是多少,它可以像这样简单:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50.0f;
}
EDIT: As per the comment, here's the result of your code and defining the header height:
编辑:根据评论,这是您的代码和定义标题高度的结果:
EDIT 2: If you want red text with a black background, change your code for tableView:viewForHeaderInSection:
like this:
编辑 2:如果您想要带有黑色背景的红色文本,请tableView:viewForHeaderInSection:
像这样更改您的代码:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
tempView.backgroundColor=[UIColor blackColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
tempLabel.text=@"Header Text";
[tempView addSubview:tempLabel];
[tempLabel release];
return tempView;
}
EDIT 3: Ok, so I'll try to merge your code from the first method with the second one. It will look like this:
编辑 3:好的,所以我会尝试将第一种方法中的代码与第二种方法合并。它看起来像这样:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
tempView.backgroundColor=[UIColor blackColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
NSString *key = nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
key = [self.searchResults objectAtIndex:section];
}
else{
key = [self.mySections objectAtIndex:section];
}
tempLabel.text=[NSString stringWithFormat:@"%@", key];
[tempView addSubview:tempLabel];
[tempLabel release];
return tempView;
}
This should return a table view section header with the proper label and proper look.
这应该返回一个带有正确标签和正确外观的表视图部分标题。
EDIT 4: Just a note on how all of this works: if you use tableView:viewForHeaderInSection:
whatever code you put in tableView:titleForHeaderInSection:
will be ignored. So you need to do the whole setup of the section header, including the proper text in the tableView:viewForHeaderInSection
method.
编辑 4:请注意所有这些是如何工作的:如果您使用您输入的tableView:viewForHeaderInSection:
任何代码,tableView:titleForHeaderInSection:
都将被忽略。因此,您需要完成部分标题的整个设置,包括方法中的正确文本tableView:viewForHeaderInSection
。