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

質問編集履歴

2

mod

2017/11/06 01:45

投稿

namenamenameko
namenamenameko

スコア234

title CHANGED
@@ -1,1 +1,1 @@
1
- diOSのTableViewでsectionの表示方法
1
+ iOSのTableViewでsectionの表示方法
body CHANGED
@@ -1,3 +1,6 @@
1
+ Swift 4.0
2
+ Xcode 9.1
3
+
1
4
  以下のような記事を参考にTableViewのセクションわけを作成しましたが、画像のように一つ目のセクション?しか表示されません。どこに問題があるのでしょうか?
2
5
  そもそも、引数の `section` なるものはどこから取ってきているのか疑問です。
3
6
 

1

mod

2017/11/06 01:45

投稿

namenamenameko
namenamenameko

スコア234

title CHANGED
@@ -1,1 +1,1 @@
1
- iOSのTableViewでsectionの表示方法
1
+ diOSのTableViewでsectionの表示方法
body CHANGED
@@ -4,4 +4,48 @@
4
4
  http://docs.fabo.io/swift/uikit/019_sectionuitableview.html
5
5
  https://i-app-tec.com/ios/tableview-cell.html
6
6
 
7
+ ```
8
+ import UIKit
9
+
10
+ class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
11
+ let firstOptions: Array = ["設定"]
12
+ let secondOptions: Array = ["設定"]
13
+ let sections: Array = ["hoge", "foo"]
14
+
15
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
16
+ let cell = tableView.dequeueReusableCell(withIdentifier: "ConfigCell", for: indexPath)
17
+ if indexPath.section == 0 {
18
+ cell.textLabel?.text = "hogehoge"
19
+ } else if indexPath.section == 1 {
20
+ cell.textLabel?.text = "hogehoge"
21
+ }
22
+ return cell
23
+ }
24
+
25
+ func numberOfSectionsInTableView(tableView: UITableView) -> Int {
26
+ return self.sections.count
27
+ }
28
+
29
+ func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
30
+ return sections[section]
31
+ }
32
+
33
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
34
+ if section == 0 {
35
+ return firstOptions.count
36
+ } else if section == 1 {
37
+ return secondOptions.count
38
+ } else {
39
+ return 0
40
+ }
41
+ }
42
+
43
+ override func viewDidLoad() {
44
+ self.navigationItem.title = "設定"
45
+ super.viewDidLoad()
46
+ }
47
+ }
48
+
49
+ ```
50
+
7
51
  ![画面](2c7368d445766ecda6f3fb3ad7223f38.jpeg)