tableView.dequeueReusableCellWithIdentifierメソッドの役割ついて
tableView.dequeueReusableCellWithIdentifierメソッドの引数に与えたindexPathは何を表すのでしょうか?
UITableViewのセルの使いまわしについて
Obj-Cで古い記事ですがセルの使い回しに付いて書かれているので
dequeueReusableCellWithIdentifier
メソッドの意味がわかると思います。
dequeueReusableCellWithIdentifier:forIndexPath:
は使い回すIndexPathを渡しています。
渡されてきたNSIndexPathは次に画面に現れるセルの番号(Sectionとrow)なのでその番号をメソッドに渡して処理をしています。
変数todoListは文字列型の配列ですのでtodoList[indexPath.row]のindexPath.rowの部分は整数だと思います。rowについてはこれも前回の質問で教わりましたが、indexPath.row←このような書き方だと何番目のrowかは特に指定されていないと思うのですがどういう基準で整数が選ばれているのでしょうか?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
というUITableViewDataSourceに定義されているメソッドは初回のテーブル生成時(画面に見えている分のみ)、テーブルをスクロールして新しいセルが生成 or 使い回しされるタイミングで自動で呼ばれるメソッドです。
そして先ほども書きましたが、次に生成されるセルの番号(Sectionとrow)が渡されてくるので、そのセルに紐付いたデータを渡す事になります。
var dataArray = ["A", "B", "C", "D", "E"]
上の様なデータの配列があった場合に
dataArray[indexPath.row]
と記述すると生成される番号に紐付いたデータが自動で渡される事になります。
※この場合はindexPath.rowは配列数と同じになるので0〜4
が渡されて来ます。
0番目のセルは"A"、1番目のセルは"B"の様に自動でそのセルに紐付いたデータが取得されていきます。
Sectionとrowはいろいろ試行錯誤しないと分かりにくいところかと思います。
以下にSection分けしたシンプルなUITableViewのコードを貼っておくのでそのまま実行して動きを確かめてみてください。
swift
1import UIKit
2
3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
4
5 var myTableView: UITableView!
6
7 // Section Header
8 let sectionTitleArray = ["Alphabet Num","Number","Alphabet Small","Alphabet Capital"]
9
10 // Data Array
11 var dataArray1 = ["One","Two","Three","Four","Five"]
12 var dataArray2 = ["1","2","3","4","5"]
13 var dataArray3 = ["a","b","c","d","e"]
14 var dataArray4 = ["A",",B","C","D","E"]
15 var dataArrayGroup: [[String]] = []
16
17 override func viewDidLoad() {
18 super.viewDidLoad()
19
20 myTableView = UITableView(frame: UIScreen.mainScreen().bounds, style: .Plain)
21 myTableView.delegate = self
22 myTableView.dataSource = self
23 view.addSubview(myTableView)
24
25 // Make Data
26 dataArrayGroup.append(dataArray1)
27 dataArrayGroup.append(dataArray2)
28 dataArrayGroup.append(dataArray3)
29 dataArrayGroup.append(dataArray4)
30
31 myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
32 }
33
34 // MARK: - TableView Delegate & DataSource
35
36 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
37 return sectionTitleArray[section]
38 }
39
40 // Section Count
41 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
42 return dataArrayGroup.count
43 }
44
45 // Row Count
46 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
47 return dataArrayGroup[section].count
48 }
49
50 // Row Height
51 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
52 return 44
53 }
54
55 // Generate Cell
56 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
57 print("indexPath.section = \(indexPath.section), indexPath.row = \(indexPath.row)")
58
59 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
60 let dataArray = dataArrayGroup[indexPath.section]
61 cell.textLabel?.text = dataArray[indexPath.row]
62 return cell
63 }
64
65 // Select Cell
66 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
67 tableView.deselectRowAtIndexPath(indexPath, animated: true)
68 }
69}
実行イメージ
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/24 07:18