swift テーブル1画面2つ上下表示
それぞれのテーブルを判別できるようにしておく必要がある。 そのためクラスを2つ作成した。同じファイル内に(継承はしていない) まずアクセスするために下のテーブル(別クラス)のインスタンス生成
上のテーブルのクラス
class Dic_iphone: UIViewController, UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate{ //下のテーブルのクラスのインスタンス var table_down = Table_down() //テーブルの名前を分けて接続、 //上のテーブル @IBOutlet weak var listTable: UITableView! //下のテーブル @IBOutlet weak var detailTable: UITableView! //viewDidloadにデリゲート宣言 override func viewDidLoad() { super.viewDidLoad() //上のテーブル listTable.delegate = self listTable.dataSource = self //下のテーブルのデリゲートここがポイントといえばポイント detailTable.delegate = table_down detailTable.dataSource = table_down
下のテーブルクラス
class Table_down :NSObject, UITableViewDelegate, UITableViewDataSource{ 通常のデリゲートメソッドを記載 接続は上のテーブルのクラスにて行う。 //セルに表示 func numberOfSections(in tableView: UITableView) -> Int { return 1 } //datasource func func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return datDics_down.count } //セルの内容表示 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! DetailCustomCell
この下のテーブルを扱うクラスには特別な処理はなにもない。
以上