如何更改iPhone SDK中Grouped TableView中的Section Headers的文本颜色?

时间:2010-12-09 05:30:23

标签: iphone objective-c cocoa-touch uitableview ios4

我正在制作一个iPhone应用程序,其中我有一个分组的TableView,其中包含各个部分的标题。

问题是我想要更改Section Header的文本颜色。

如何更改Section Header的文本颜色?

我该怎么办?

7 个答案:

答案 0 :(得分:52)

将以下代码添加到- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中的AppDelegate类 方法:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];

答案 1 :(得分:33)

这对你很有用。

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

只需在代码中复制并粘贴此功能即可。

答案 2 :(得分:33)

如果您不想像在Vahan的解决方案中那样使用app,那么这是使用UITableViewDelegate方法之一的解决方案:

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
       headerView.textLabel?.textColor = UIColor.redColor() 
    }
}

答案 3 :(得分:3)

您可以实现此表视图数据源方法:

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
   //create your custom label here & anything else you may want to add
   return YourCustomView;
}

答案 4 :(得分:2)

我建立了@Harsh的答案。

这是我能得到的最接近的,与我能说的无法区分。

它位于< UITableViewDataSource>明显。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    hView.backgroundColor=[UIColor clearColor];

    UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];

    hLabel.backgroundColor=[UIColor clearColor];
    hLabel.shadowColor = [UIColor whiteColor];
    hLabel.shadowOffset = CGSizeMake(0.5,1);  // closest as far as I could tell
    hLabel.textColor = [UIColor blackColor];  // or whatever you want
    hLabel.font = [UIFont boldSystemFontOfSize:17];
    hLabel.text = @"Your title here";  // probably from array

    [hView addSubview:hLabel];

    return hView;
}

答案 5 :(得分:2)

@Harsh的回答对我很有帮助,通过改变UILabel的协调,你可以移动它。另外,我个人认为稍微改变阴影偏移以使其更具可读性,但这可能是个人选择。这是我的版本:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
    //If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor yellowColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.5., 0.5.);
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
    [view addSubview:label];

    return view;
}

答案 6 :(得分:2)

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UILabel *myLabel = [[UILabel alloc] init];
  myLabel.frame = CGRectMake(20, 8, 220, 20);
  myLabel.font = [UIFont boldSystemFontOfSize:16];
  myLabel.text = [self tableView:tableView 
  titleForHeaderInSection:section];
  myLabel.backgroundColor=[UIColor grayColor];
  UIView *headerView = [[UIView alloc] init];
  [headerView addSubview:myLabel];
  return headerView;
  }