0

我有一个UITableViewwith 部分和我设置的标题,titleForHeaderInSection如下所示:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return [NSString stringwithFormat :@"Section %d",section];
}

默认情况下,它带有灰色背景,文本颜色为白色。如何将其更改为其他颜色。?

通过谷歌,发现设置在viewForHeaderinSection. 但我没有用它来设置标题。所以我不想在里面写。

我怎样才能把它写进去titleForHeaderInSection

4

4 回答 4

2

在 iOS 6 及更高版本中

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont systemFontOfSize:18.0f]];
[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setBackgroundColor:[UIColor redColor]];
于 2013-09-04T15:22:38.847 回答
1
   - (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;
    }
于 2013-01-19T12:06:30.203 回答
1

titleForHeaderInSection:此委托UITableView仅用于设置文本。您可以在下面看到它的返回类型 it NSString

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // fixed font style. use custom view (UILabel) if you want something different
}

为了实现节标题的自定义视图,您唯一的选择是viewForHeaderInSection委托方法。titleForHeaderInSection我可以保证您无法使用此外,您无法设置标题的视图,其行为与您可以看到的实现方式viewForHeaderInSection完全相同:titleForHeaderInSection

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Customized your view however you want.

    return myCustomizedView;
}
于 2013-01-19T12:34:04.083 回答
0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[[UIView alloc]initWithFrame:CGRectMake(0,0,300,25)]autorelease];
    tempView.backgroundColor=[UIColor grayColor];
    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,300,25)];
    tempLabel.backgroundColor=[UIColor clearColor];
    NSString * headerText = [NSString stringWithFormat:@"%d",section];
    tempLabel.text= headerText;
    [tempView addSubview: tempLabel];
    [tempLabel release];
    return tempView;
}

- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}
于 2013-01-19T12:16:10.380 回答