2017-09-20 9 views
1

以下のマイコードは典型的なテーブルビューです。私がしたいのは、それぞれのセルセルとdxの両方にvarテキストを表示する2つのテーブルビューを持つことです。私は "return cell & & cc"を使って結合しようとしましたが、それは動作しません。私は、2つの異なるテーブルビュー内のセルを分離して、同じ変数を同じビューコントローラに表示することができるようにしたいだけです。同じビューコントローラで2つのテーブルビューを使用する(swift3)

 import UIKit 

var text = ["a", "b"] 

var row = 0 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet var b: UITableView! 
    @IBOutlet var a: UITableView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == a { 


      return 5 
     } 
     else { 

      return 10 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if tableView == a { 
      let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
      return cell 
     } 
     else { 
      let cell = UITableViewCell(style: .default, reuseIdentifier: "dx") 
      return cell 

     } 
    } 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
row = indexPath.row 
}} 

enter image description here

答えて

2

次のように次の2つのテーブルビューを使用することができます - >

IBOutlet

UITableViewDataSource、UITableViewDelegate

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == table1 { 
     return 5 
    } 
    else { 
     return 10 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if tableView == table1 { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
     return cell 
    } 
    else { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell1") 
     return cell 

    } 
} 
+0

私はそれを構築し、あなたの提案でコードを編集しました。ただし、いずれのテキストビューにもvarテキストは表示されません。 –

+0

コード提案は上記の私の編集した質問に反映されています。 –

0

UITableViewDataSource

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var count:Int? 

    if tableView == self.tblGuests { 
     count = 2 
    } 

    if tableView == self.tblDetail{ 
     count = 5 
    } 

    return count! 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell:UITableViewCell? 

     if tableView == self.tblGuests { 
      cell = tableView.dequeueReusableCell(withIdentifier: "BookingGuestsCell", for: indexPath) as! BookingGuestsCell 
     } else { 
      cell = tableView.dequeueReusableCell(withIdentifier: "BookingRoomDetailCell", for: indexPath) as! BookingRoomDetailCell 
     } 
     return cell! 
} 
関連する問題