在章节中更改titleFor标题的颜色

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { } 

你好
我是非常新的目标c …………………..通过这种方法,我们可以得到标题部分的标题,但如何改变颜色string?我可以这样做……..如果有人知道,请告诉我。

问候

请注意,您返回的视图不保留UITableView(OS 3.1.2至less似乎显示此问题)。 这导致在执行到viewDidLoad之前发生难以find的崩溃。

表格视图不会按照您的想法在需求上抓取您的视图。 它要求他们所有,然后再次要求他们,有时几次,所以每个请求生成它们是非常低效的。

您可以尝试folowing: 标题与两个自定义标签

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

使用tableView:viewForHeaderInSection:来代替。 这样,你可以configuration一个UILabel或任何你喜欢的(完整的字体,透明度,颜色等),tableview将使用该视图作为标题,而不是默认视图。

请使用以下代码并更改UITableView标题的颜色

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *tempHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,44)]; tempHeaderView.backgroundColor=[UIColor clearColor]; UILabel *tempHeaderLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,320,44)]; tempHeaderLabel.backgroundColor=[UIColor clearColor]; tempHeaderLabel.text=@"HEADER"; [tempHeaderView addSubView: tempHeaderLabel]; return tempHeaderView; }