質問

UITableViewでセクションヘッダーの色を変更するにはどうすればよいですか

編集:iOS 6および上記。受け入れられた回答は古くなっています。

役に立ちましたか?

解決

うまくいけば、 UITableViewDelegate プロトコルからのこのメソッドがあなたを始めるでしょう:

Objective-C:

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

Swift:

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
  let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
  if (section == integerRepresentingYourSectionOfInterest) {
    headerView.backgroundColor = UIColor.redColor()
  } else {
    headerView.backgroundColor = UIColor.clearColor()
  }
  return headerView
}

2017年更新:

Swift 3:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
        if (section == integerRepresentingYourSectionOfInterest) {
            headerView.backgroundColor = UIColor.red
        } else {
            headerView.backgroundColor = UIColor.clear
        }
        return headerView
    }

[UIColor redColor] を任意の UIColor に置き換えます。 headerView の寸法を調整することもできます。

他のヒント

これは古い質問ですが、答えを更新する必要があると思います。

この方法では、独自のカスタムビューを定義および作成する必要はありません。 iOS 6以降では、

を定義することにより、背景色とテキストの色を簡単に変更できます
-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)section

セクションデリゲートメソッド

例:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

ここに私の投稿から引用: https://happyteamlabs.com/ blog / ios-how-to-customize-table-view-header-and-footer-colors /

Swift 3/4

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}

テキストの色を変更する方法は次のとおりです。

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];

カスタムカラーのヘッダーが必要な場合は、これを実行できます。

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

このソリューションは、iOS 6.0以降で問題なく機能します。

次のソリューションは、 Swift 1.2 with iOS 8 + で機能します

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This changes the header background
    view.tintColor = UIColor.blueColor()

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour
    var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel.textColor = UIColor.redColor()

}

デリゲートからこのコードを追加することを忘れないでください。追加すると、ビュー/ラベルの高さに対して、ビューが切り取られたり、テーブルの後ろに表示されることがあります。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

カスタムビューを作成したくない場合は、次のように色を変更することもできます(iOS 6が必要です):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}

main.storyboardで約2秒で実行できます。

  1. テーブルビューを選択
  2. 属性インスペクターに移動
  3. リストアイテム
  4. 下にスクロールして[小見出しを表示]
  5. "バックグラウンド"を変更する

こちらをご覧ください

UITableViewHeaderFooterViewの背景色の設定は廃止されました。代わりに contentView.backgroundColor を使用してください。

セクション領域の背景とテキストの色を設定します:( William Jockusch Dj S に感謝)

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        castView.contentView.backgroundColor = [UIColor grayColor];
        [castView.textLabel setTextColor:[UIColor grayColor]];
    }
}

Swift 4

UITableViewセクションのヘッダービューの背景色テキストラベルの色、およびフォントを変更するには、単に willDisplayHeaderViewをオーバーライドしますのようなテーブルビューの場合:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.backgroundView?.backgroundColor = .white
        header.textLabel?.textColor = .black
        header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
} 

これは私にとって完璧に機能しました。それがあなたにも役立つことを願っています!

ヘッダービューで画像を追加する方法は次のとおりです。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];

    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

    [headerView addSubview:headerImage];

    return headerView;
}

iOS8(ベータ)およびSwiftの場合は、必要なRGBカラーを選択して、これを試してください:

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()

    header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
    return header

}

(プロジェクトで通常のUIViewControllerではなくUITableViewControllerを使用しているため、「オーバーライド」がありますが、セクションヘッダーの色を変更するために必須ではありません)

ヘッダーのテキストは引き続き表示されます。 セクションヘッダーの高さを調整する必要があることに注意してください。

幸運。

SWIFT 2

ぼかし効果を追加して、セクションの背景色を正常に変更することができました(これは非常にクールです)。セクションの背景色を簡単に変更するには:

  1. まずストーリーボードに移動して、テーブルビューを選択します
  2. 属性インスペクターに移動
  3. リストアイテム
  4. 下にスクロールして表示
  5. 「背景」を変更

その後、ぼかし効果のために、コードに追加します:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This is the blur effect

    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
    let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel!.textColor = UIColor.darkGrayColor()
    headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
    headerView.tintColor = .groupTableViewBackgroundColor()
    headerView.backgroundView = blurEffectView

}

その答えは知っていますが、念のため、Swiftでは次のものを使用します

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let tableViewWidth = self.tableView.bounds

        let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
        headerView.backgroundColor = UIColor.greenColor()

        return headerView
    }

iOS 8以降

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}

@Dj Sの回答に基づき、Swift 3を使用。これはiOS 10でうまく機能します。

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Background color
    view.tintColor = UIColor.black

    // Text Color
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.textColor = UIColor.white
}

iOS 7.xで静的テーブルビューセルを使用するプロジェクトがあります。 willDisplayHeaderViewは発生しません。ただし、この方法は正常に機能します。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSLog(@"%s", __FUNCTION__);
    CGRect headerFrame = CGRectMake(x, y, w, h);    
    UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];  
    headerView.backgroundColor = [UIColor blackColor];
 -(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view
  forSection:(NSInteger)section
  {
        if ([view isKindOfClass: [UITableViewHeaderFooterView class]])
        {
             UITableViewHeaderFooterView *castView = (UITableViewHeaderFooterView *) view;
             UIView *content = castView.contentView;
             UIColor *color = [UIColor whiteColor]; // substitute your color here
             content.backgroundColor = color;
             [castView.textLabel setTextColor:[UIColor blackColor]];
        }
 }

このコードはそれほど悪くないと思います。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.whiteColor()
    headerView.backgroundView = backgroundView
    headerView.textLabel.text = "hello"
    return headerView
}

iOS 7.0.4では、独自のXIBでカスタムヘッダーを作成しました。前にここで言及したことは何も働きませんでした。 dequeueReusableHeaderFooterViewWithIdentifier:を使用するには、UITableViewHeaderFooterViewのサブクラスである必要があり、クラスは背景色に関して非常に頑固であるようです。そのため、最後にcustomBackgroudViewという名前のUIView(コードまたはIBで実行できます)を追加し、そのbackgroundColorプロパティを設定します。 layoutSubviews:ビューのフレームを境界に設定します。 iOS 7で動作し、不具合はありません。

// in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
// first child becomes contentView

// in MyTableHeaderView.h
@property (nonatomic, weak) IBOutlet UIView * customBackgroundView;

// in MyTableHeaderView.m
-(void)layoutSubviews;
{
    [super layoutSubviews];

    self.customBackgroundView.frame = self.bounds;
}
// if you don't have XIB / use IB, put in the initializer:
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    ...
    UIView * customBackgroundView = [[UIView alloc] init];
    [self.contentView addSubview:customBackgroundView];
    _customBackgroundView = customBackgroundView;
    ...
}


// in MyTableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyTableHeaderView * header = [self.tableView
                                          dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
    header.customBackgroundView.backgroundColor = [UIColor redColor];
    return header;
}

ヘッダービューのレイヤーの色を変更するだけ

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,    tableView.bounds.size.width, 30)] autorelease];
 headerView.layer.backgroundColor = [UIColor clearColor].CGColor
}

誰もが迅速に必要な場合、タイトルを保持します:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
    view.backgroundColor = UIColor.redColor()
    let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
    label.text = self.tableView(tableView, titleForHeaderInSection: section)
    view.addSubview(label)
    return view
}

私の場合、次のように機能しました:

let headerIdentifier = "HeaderIdentifier"
let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
header.contentView.backgroundColor = UIColor.white

背景ビューの背景色を設定するだけです:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){         
  let tableHeader = view as! UITableViewHeaderFooterView        
  tableHeader.backgroundView?.backgroundColor = UIColor.white     
}

RubyMotion / RedPotionを使用して、これをTableScreenに貼り付けます:

  def tableView(_, willDisplayHeaderView: view, forSection: section)
    view.textLabel.textColor = rmq.color.your_text_color
    view.contentView.backgroundColor = rmq.color.your_background_color
  end

魅力のように機能します!

コンソールログからXcodeからメッセージを受け取りました

  

[TableView]背景色をオンに設定   UITableViewHeaderFooterViewは非推奨になりました。カスタムを設定してください   backgroundViewに希望する背景色を使用したUIView   代わりにプロパティ。

次に、新しいUIViewを作成し、HeaderViewの背景として配置します。 良い解決策ではありませんが、Xcodeが言ったように簡単です。

func tableView(_ tableView:UITableView、willDisplayHeaderViewビュー:UIView、forSectionセクション:Int)も機能しますが、別のデリゲートメソッドを実装せずにこれを実現できます。 あなたの func tableView(_ tableView:UITableView、viewForHeaderInSectionセクション:Int)-> UIView?メソッドでは、 view.backgroundView?.backgroundColor = UIColor.white の代わりに view.contentView.backgroundColor = UIColor.white を使用できます。 。 ( backgroundView がオプションであることは知っていますが、それが存在する場合でも、 willDisplayHeaderView

を実装しない限り、これは起動しません。

UIAppearanceを使用すると、アプリケーションのすべてのヘッダーで次のように変更できます。

UITableViewHeaderFooterView.appearance()。backgroundColor = theme.subViewBackgroundColor

Swift 4は非常に簡単です。これをクラスに追加し、必要に応じて色を設定するだけです。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }

または単純な色の場合

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor.white
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top