テーブルの要素(行)をSwift内で登録したいのです。
以下の例では、leftMenuの内容を出力しているのですが、ViewController内にUItableviewを複数配置する場合はどのようにすればいいのか分からず困っています。
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 6 @IBOutlet weak var tblSample: UITableView! 7 @IBOutlet weak var lblTapped: UILabel! 8 9 let leftMenu = [“apple”, “banana”, “cherry”, “dragon fruit”,] 10 11 // ここの内容を表示させたい 12 @IBOutlet weak var tblSample2: UITableView! 13 let rightMenu = [“xxx”, “yyy”, “zzz”,] 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 tblSample.delegate = self 19 tblSample.dataSource = self 20 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 } 26 27 // ★1 leftMenuの内容を返しているが、複数の場合はどうするのか? 28 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 29 return leftMenu.count 30 } 31 32 // ★2 33 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 34 35 let cell = tableView.dequeueReusableCell(withIdentifier: “beginnerCell”, for: indexPath) 36 cell.textLabel?.text = leftMenu[indexPath.row] 37 38 return cell 39 } 40 41 // ★3 42 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 43 lblTapped.text = leftMenu[indexPath.row] 44 } 45 46 47}
実際に試してみて、各種delegateでどの様な違いがあるか確認してみると良いのではないでしょうか。まぁ、delegateは必ずしも想定するControllerで実装する必要もないかと思いますが。
回答2件
あなたの回答
tips
プレビュー