質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

3904閲覧

swift3 tableViewのセクションを増やす場合のコード

kifu

総合スコア26

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

2クリップ

投稿2016/12/19 14:01

編集2016/12/20 01:25

// Tableで使用する配列を定義する.
var myCoatItems: NSArray = ["トレンチ", "ダッフル", "ダウン", "スプリング", "ステンカラー"]
var myJyacketItems: NSArray = ["ブレザー", "ジャンパー", "ブルゾン", "パーカー"]

// Sectionで使用する配列を定義する.

var mySections: NSArray = ["コート", "ジャケット"]

上記の3つの配列でtableViewにセクションごとの表示をさせようと思っています。そして、Sectionを含むデータを追加したいと考えています。この場合、下記のsection == 0 、section == 1 を任意の数nまで増やしていくのには、section == 0や section == 1の部分はどのように書けば良いのでしょうか。
ネットでセクションとセルをtableViewに表示させるコードを探したところ、見た範囲ではこのようになっていたものですから...。countを使うとかで簡単な方法もあるのでしょうか。if文を使って増やしていくとかなのであれば、どのように書けるのでしょうか。ご回答にいつも心から感謝しています。

セクションの数を返す. */ func numberOfSectionsInTableView(tableView: UITableView) -> Int { return mySections.count } /* セクションのタイトルを返す. */ func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return mySections[section] as? String } /* Cellが選択された際に呼び出される. */ func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 0 { print("Value: \(myCoatItems[indexPath.row])") } else if indexPath.section == 1 { print("Value: \(myJyacketItems[indexPath.row])") } } /* テーブルに表示する配列の総数を返す. */ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return myCoatItems.count } else if section == 1 { return myJyacketItems.count } else { return 0 } } /* Cellに値を設定する. */ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) if indexPath.section == 0 { cell.textLabel?.text = "\(myCoatItems[indexPath.row])" } else if indexPath.section == 1 { cell.textLabel?.text = "\(myJyacketItems[indexPath.row])" } return cell }
KCROW👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2016/12/20 00:56

コードは ``` で囲って下さい
kifu

2016/12/20 01:11

```で囲いました。御依頼に沿っているでしょうか。御指導ありがとうございます。
fuzzball

2016/12/20 01:12

閉じる方の ``` の前にスペースが入っているので削除して下さい。
kifu

2016/12/20 01:26

削除しました。御指導ありがとうございます。
guest

回答2

0

二次元配列を使いましょう。
アクセス例を書いておきますので後はがんばって下さい。

swift

1let items = [ 2 ["トレンチ", "ダッフル", "ダウン", "スプリング", "ステンカラー"], //#0 coat 3 ["ブレザー", "ジャンパー", "ブルゾン", "パーカー"], //#1 jacket 4] 5 6print(items[0][0]) 7//=> トレンチ 8 9print(items[1][2]) 10//=> ブルゾン 11 12print(items.count) 13//=> 2 ※種類数 14 15print(items[0].count) 16//=> 5 ※コートの種類数

投稿2016/12/20 01:11

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kifu

2016/12/20 10:30

頑張ります。ありがとうございました。
guest

0

ベストアンサー

以下の様に記述するとsectionn個の場合も処理をわけなくて良いですよ。

swift

1import UIKit 2 3class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 4 5 var myCoatItems = ["トレンチ", "ダッフル", "ダウン", "スプリング", "ステンカラー"] 6 var myJyacketItems = ["ブレザー", "ジャンパー", "ブルゾン", "パーカー"] 7 8 var sectionArray: Array<Array<String>> = [[]] 9 var mySections = ["コート", "ジャケット"] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 // セクションのArrayを作成 15 sectionArray = [myCoatItems, myJyacketItems] 16 } 17 18 /* 19 セクションの数を返す. 20 */ 21 func numberOfSections(in tableView: UITableView) -> Int { 22 return mySections.count 23 } 24 25 /* 26 セクションのタイトルを返す. 27 */ 28 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 29 return mySections[section] 30 } 31 32 /* 33 Cellが選択された際に呼び出される. 34 */ 35 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 36 print(sectionArray[indexPath.section][indexPath.row]) 37 } 38 39 /* 40 テーブルに表示する配列の総数を返す. 41 */ 42 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 43 return sectionArray[section].count 44 } 45 46 /* 47 Cellに値を設定する. 48 */ 49 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 50 51 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) 52 cell.textLabel?.text = sectionArray[indexPath.section][indexPath.row] 53 return cell 54 } 55}

投稿2016/12/19 14:28

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kifu

2016/12/20 10:31

ありがとうございます。感謝します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問