UITableView - セクションヘッダーの色を変更する 質問する

UITableView - セクションヘッダーの色を変更する 質問する

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

編集DJ-Sによる回答iOS 6 以降で考慮する必要があります。承認された回答は古くなっています。

ベストアンサー1

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

この方法では、独自のカスタムビューを定義して作成する必要はありません。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/

スイフト 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
}

おすすめ記事