Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 更改UITableView中sectiontitle的背景色_Iphone_Uitableview_Xcode4.5 - Fatal编程技术网

Iphone 更改UITableView中sectiontitle的背景色

Iphone 更改UITableView中sectiontitle的背景色,iphone,uitableview,xcode4.5,Iphone,Uitableview,Xcode4.5,我有一个UITableView,其中包含节,我正在titleForHeaderInSection中为节设置标题,如下所示: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringwithFormat :@"Section %d",section]; } 默认为灰色背景,文本颜色为白色。如何将其更改为其他颜色 通过

我有一个
UITableView
,其中包含节,我正在
titleForHeaderInSection
中为节设置标题,如下所示:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return [NSString stringwithFormat :@"Section %d",section];
}
默认为灰色背景,文本颜色为白色。如何将其更改为其他颜色

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


如何将其写入标题部分?使用以下代码可能会对您有所帮助:)

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

- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

使用以下代码可能对您有所帮助:)

- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // fixed font style. use custom view (UILabel) if you want something different
}
为了实现节标题的自定义视图,您唯一的选项是
viewForHeaderInSection
delegate方法。我可以保证,您无法使用
titleForHeaderInSection
设置标题视图,
viewForHeaderInSection
的作用与
titleForHeaderInSection
的作用完全相同,您可以看到如下实现:

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

    return myCustomizedView;
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // fixed font style. use custom view (UILabel) if you want something different
}
为了实现节标题的自定义视图,您唯一的选项是
viewForHeaderInSection
delegate方法。我可以保证,您无法使用
titleForHeaderInSection
设置标题视图,
viewForHeaderInSection
的作用与
titleForHeaderInSection
的作用完全相同,您可以看到如下实现:

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

    return myCustomizedView;
}
在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]];
在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]];

回答不错但无法显示标题:(回答不错但无法显示标题:(