teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

ソース全文を回答頂いた内容に更新

2017/02/12 10:15

投稿

Aki_1988
Aki_1988

スコア44

title CHANGED
File without changes
body CHANGED
@@ -24,8 +24,8 @@
24
24
  self.taskList.tableHeaderView = headerView
25
25
  ```
26
26
 
27
- ###全文
27
+ ###ソース
28
- 関係ありそうな箇所のみ抜粋してます。
28
+ 回答頂いたもをもとに更新しております。
29
29
  ```
30
30
  import UIKit
31
31
 
@@ -33,10 +33,7 @@
33
33
 
34
34
  override func viewDidLoad() {
35
35
  super.viewDidLoad()
36
- let headerView = self.taskList.tableHeaderView as UIView!
37
- headerView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 500)
36
+ self.navigationItem.titleView = UIImageView(image:UIImage(named:"text_nav_title_today"))
38
- self.taskList.tableHeaderView = headerView
39
-
40
37
  }
41
38
 
42
39
  override func didReceiveMemoryWarning() {
@@ -48,11 +45,10 @@
48
45
 
49
46
  @IBOutlet weak var taskList: UITableView!
50
47
 
51
- //テーブルデータ
48
+ //データ
52
-
53
- var taskTitles = [["task01","task02"],["task03"]]
49
+ var taskTitles = [["task01","task02"],["task03"]]
54
- var taskSubTitles = [["subtask01","subtask02"],["subtask03"]]
50
+ var taskSubTitles = [["subtask01","subtask02"],["subtask03"]]
55
- var taskSections:[String] = ["projectA","projectB"]
51
+ var taskSections:[String] = ["projectA","projectB"]
56
52
 
57
53
 
58
54
  //テーブル定義
@@ -71,14 +67,32 @@
71
67
 
72
68
  //section
73
69
  func tableView(tableView:UITableView, titleForHeaderInSection section:Int) -> String?{
74
- let label = UILabel(frame: CGRect(x:0, y:0, width: tableView.bounds.width, height: 50))
75
- label.text = taskSections[section]
70
+ return taskSections[section]
76
- label.backgroundColor = UIColor.redColor()
77
- return label.text
78
71
  }
79
72
 
80
73
  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
81
74
  return taskSections.count
82
- }
75
+ }
76
+
77
+
78
+ // Section Header View
79
+ func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
80
+ // HeaderのViewを作成してViewを返す
81
+ let headerView = UIView()
82
+ let label = UILabel()
83
+ label.text = "セクション"
84
+ label.textColor = UIColor.whiteColor()
85
+ headerView.backgroundColor = UIColor.redColor()
86
+ // いろいろ・・・
87
+
88
+ headerView.addSubview(label)
89
+ return view
90
+ }
91
+
92
+ // Section Header Height
93
+ func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
94
+ // ヘッダーViewの高さを返す
95
+ return 40
96
+ }
83
97
  }
84
98
  ```

1

キャプ等追加

2017/02/12 10:15

投稿

Aki_1988
Aki_1988

スコア44

title CHANGED
@@ -1,1 +1,1 @@
1
- UItableViewのセクション背景/文字色を変更したい。
1
+ UITableViewのセクションヘッダー背景/文字色を変更したい。
body CHANGED
@@ -1,8 +1,10 @@
1
1
  ###前提・実現したいこと
2
- 現在swiftを勉強中です。UItableViewでセクション分けとセクション部分のカスタマイズを行いたいのですが、具体的なやり方がわからず困っております。
2
+ 現在swiftを勉強中です。UITableViewでセクション分けとセクション部分のカスタマイズを行いたいのですが、具体的なやり方がわからず困っております。
3
3
  具体的には、セクションの背景色と文字色を変更できればと考えております。
4
4
  初歩的な質問かと思いますが、よろしくお願い致します。
5
5
 
6
+ ![イメージ説明](25e0ee6606dc44d073e6349c9240c556.png)
7
+
6
8
  ###試したこと
7
9
  ①セクションにlabelを設定して、それの背景色を設定する、を試しましたがこの関数では、Stringしか返せず。
8
10
  返り値をUIViewにすると「objective-C method~」のようなエラー表示。