LoginSignup
13

More than 5 years have passed since last update.

【Swift】tableViewのセクションヘッダの背景色や文字色を変更する方法

Posted at

所感

tableViewのセクションの色を変えたいと思い、かなり探したのですがなかなか見つからなくて困ってました。やはり、英語のキーワードのみで検索するほうが出てくる気がしますね。
http://stackoverflow.com/questions/25900577/how-to-set-a-color-in-a-titleforheaderinsection-swift
上記URLを参考にしてセクションの設定を変更しました。

変更方法

var sections = [String]() // セクション名を格納しておく

//この関数内でセクションの設定を行う
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label : UILabel = UILabel()
        label.backgroundColor = UIColor.grayColor()
        label.textColor = UIColor.whiteColor()
        if(section == 0){
            label.text = sections[section]
        } else if (section == 1){
            label.text = sections[section]
        }
        return label
}

自分の場合は、illustratorなどで色の感じを見てからアプリを作っていたりするので、カラーコードで設定できたほうが嬉しかったりします。調べてみるとドンピシャのもの発見。
http://swift-salaryman.com/uicolorutil.php
こちらを参考にするといい感じに出来ました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13