【问题标题】:How to change titleForHeader background color in iOS?如何在 iOS 中更改 titleForHeader 背景颜色?
【发布时间】:2013-01-15 20:55:39
【问题描述】:

我想更改 iOS 中的 titleForHeader 背景颜色。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
     return @"A";
}

此方法显示UITableView 中的titleHeader 部分。

默认背景色为浅蓝色。

我想改变这些背景的黑色。

我该怎么办?

【问题讨论】:

标签: ios cocoa-touch uitableview


【解决方案1】:

看看 Alex Reynolds 在这里的回答

UITableView - change section header color

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
      UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
      if (section == integerRepresentingYourSectionOfInterest)
         [headerView setBackgroundColor:[UIColor redColor]];
      else 
         [headerView setBackgroundColor:[UIColor clearColor]];


 return headerView;
}

如果你只得到背景颜色但没有文字

查看 DoctorG 对同一问题的回答

       UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
    label.text = @"Section Header Text Here";
    label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];

label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];

【讨论】:

  • integerRepresentingYourSectionOfInterest 是什么兄弟?我需要在哪里申报?
  • 你有多少个部分?这意味着一个整数,例如对于您的第一部分使用 0 if (section == 0) 第二部分 if (section == 1) 等等
猜你喜欢
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 2020-05-07
  • 2011-11-11
相关资源
最近更新 更多