import UIKit let sectionTitle = ["チョウ目","バッタ目","コウチュウ目"] let tection0 = [("キタテハ","タテハチョウ科"),("クロアゲハ","アゲハチョウ科")] let section1 = [("キリギリス","キリギリス科"),("ヒナバッタ","バッタ科"),("マツムシ","マツムシ科")] let section2 = [("ハンミョウ","ハンミョウ科"),("アオオサムシ","オサムシ科"),("チビクワガタ","クワガタムシ科")] let tableData = [tection0,section1,section2] class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() let myTableView:UITableView! myTableView = UITableView(frame : view.frame, style : .grouped) myTableView.delegate = self myTableView.dataSource = self view.addSubview(myTableView) } func numberOfSections(in tableView: UITableView) -> Int{ return sectionTitle.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ let sectionData = tableData[section] return sectionData.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sectionTitle[section] } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell") let sectionData = tableData[(indexPath as NSIndexPath).section] let cellData = sectionData[(indexPath as NSIndexPath).row] cell.textLabel?.text = cellData.0 cell.detailTextLabel?.text = cellData.1 return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let title = sectionTitle[indexPath.section] let sectionData = tableData[indexPath.section] let cellData = sectionData[indexPath.row] print("(title)(cellData.1)") print("(cellData.0)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
swiftを学習している者です。
上記のプログラムで質問があります。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
let sectionData = tableData[section]
return sectionData.count
}
各セクションの値が入った配列「tableData」を「sectionData」に取り出して、sectionDataの個数を返していると思いますが、let sectionData = tableData[section]という文でなぜtableDataの後ろに[seciton]をつけているのかがわかりません)
お手数ですが、回答を宜しくお願いします。
「質問するときのヒント」(https://teratail.com/help/question-tips)を参考にして、コードは<code>ボタンを押してSwiftのコードとして整形表示するようにしたほうが回答者もコードが見やすくなります。

回答1件
あなたの回答
tips
プレビュー