###前提
Xcode 9.2
Swift 4.0.3
###概要
StoryboardでTable ViewとTable View Cellを配置
↓
・Table ViewのCustom Classに「SampleView」を指定
・Table ViewのStyleに「Grouped」を指定
・Table View CellのIdentifierに「Hogehoge」を指定
↓
以下のコードを書いたところ、1個目のセクションしか表示できませんでした。
lang
1// フレームワークの読み込み 2import UIKit 3 4class SampleView: UITableViewController { 5 6 // データ 7 let sectionItems: NSArray = ["セクション1", "セクション2"] 8 let cellLabels1: NSArray = ["セル1", "セル2", "セル3", "セル4"] 9 let cellLabels2: NSArray = ["セル5"] 10 11 // セクションの数 12 func numberOfSectionsInTableView(_ tableView: UITableView) -> Int { 13 return sectionItems.count 14 } 15 16 // セクションの名前 17 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 18 return sectionItems[section] as? String 19 } 20 21 // セクション内の行数 22 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 23 24 switch section { 25 case 0: 26 return cellLabels1.count 27 case 1: 28 return cellLabels2.count 29 default: 30 return 0 31 } 32 33 } 34 35 // セルのラベルを出力 36 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 37 38 let cell = tableView.dequeueReusableCell(withIdentifier: "Hogehoge", for: indexPath) 39 40 switch indexPath.section { 41 case 0: 42 cell.textLabel?.text = cellLabels1[indexPath.row] as? String 43 case 1: 44 cell.textLabel?.text = cellLabels2[indexPath.row] as? String 45 default: 46 break 47 } 48 49 return cell 50 51 } 52 53}
画像でまとめるとこんな感じ。
何か、根本的なことを理解できていないかもしれません、、、
ご教授よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/05 23:34