// 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 }
回答2件
あなたの回答
tips
プレビュー