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

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

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

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

Swift

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

Q&A

解決済

1回答

524閲覧

UITableView numberofrowsinsectionのsectionについて

power

総合スコア12

Xcode

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

Swift

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

0グッド

0クリップ

投稿2019/02/17 12:41

編集2019/02/17 13:38
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]をつけているのかがわかりません)

お手数ですが、回答を宜しくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

numberOfRowsInSectionは指定したSectionに対して、Rowが何件あるかを返すものです。

Swift

1let tableData = [tection0,section1,section2]

となっているので、Sectionが0の際はtection0、1の際はsection1、2の際はsection2の配列が取得できます。
その配列のcountを返すことによってSection内にあるRowの件数としています。

例えば0の場合、sectionDataに代入されるのはtection0になります。
sectionData.countによってtection0の件数である2が返ります。

投稿2019/02/17 13:57

nakasho_dev

総合スコア2655

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

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

power

2019/02/18 04:05

ご回答、ありがとうございます!理解いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問