15

InAppSettingsKitでUITableViewのスタイルを設定していて、ヘッダータイトルの色を変更したい:

テーブルビューの画像

ラベルWithout titleText Fieldは白である必要があります。これはどのように行うことができますか?

ありがとう。

4

6 に答える 6

66

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

この方法では、独自のカスタムビューを定義および作成する必要はありません。iOS 6以降では、を定義することで、背景色とテキストの色を簡単に変更できます。

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)

デリゲートメソッド。

例えば:

- (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 5.0:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel?.textColor = .white
    }
}
于 2013-10-04T05:15:54.077 に答える
5

tableView:viewForHeaderInSection:tableViewControllerにメソッドを実装します。これにより、ヘッダーに独自のビューを提供できるようになります。これには、必要なフォーマットのUILabelを含めることができます。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *customLabel = [[UILabel alloc] init];
    customLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    return customLabel;
}

ラベルフレームは、セクションの間隔を空けるのに十分な高さに設定することを忘れないでください。より大きなUIView内にラベルを埋め込み、代わりにそれを返すことで、配置を簡素化することができます(たとえば、ラベルの左パディングを増やしたい場合)。

于 2012-01-24T17:18:55.913 に答える
4

これをSwiftに「変換」するのに数分かかりましたが、これはSwift 1.2(iOS 8)で機能する同等のものです。必ずクラスに実装UITableViewDelegateしてください。

// MARK: - VIEW METHODS
override func viewDidLoad() {
    tableView.delegate = self
}

// MARK: - TABLEVIEW DELEGATE METHODS
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel.textColor = UIColor.whiteColor()
}
于 2015-06-30T19:19:25.580 に答える
1

私はこれを見つけました: iPhone SDKのグループ化されたTableViewのセクションヘッダーのテキストの色を変更するにはどうすればよいですか? それはおもてなしをします、そして私はそれを@rishiによる答えのリンクからの少しのコードと組み合わせました、そしてそれは私の人生をとても楽にしてくれました!! ラベルビューの調整を少し調整する必要がありましたが、それは魅力のように機能しました。

于 2012-08-30T00:27:13.250 に答える
1

Swift 4バージョン:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let headerView = view as? UITableViewHeaderFooterView else { return }
    headerView.textLabel?.textColor = .red // any color
}
于 2019-08-08T16:36:11.630 に答える
0

テーブルセルの作成方法で次のコード行を試すことができます-

cell.textLabel.backgroundColor = //Your color;
cell.detailTextLabel.backgroundColor = //Your color;

詳細な説明については、以下を参照してください。ここでは、言及したものと同様のカスタムセクションテーブルを作成する詳細な例を見つけることができます-http: //undefinedvalue.com/2009/08/25/changing-background-color-and-section -header-text-color-grouped-style-uitableview

于 2012-01-24T17:08:14.717 に答える