2017-09-20 61 views
1

我的代码如下是典型的tableview。我想要做的是有两个tableviews与那里各自的单元格和dx都显示var文本。我尝试使用“返回单元格& & cc”组合东西,但这不起作用。我只是希望能够使用2个不同的tableview中的单元格来在相同的视图控制器中显示相同的变量。在同一视图控制器中使用2个tableviews(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

您可以使用两个实现代码如下如下: - >

IBOutlet中

@IBOutlet var table1: UITableView! 
@IBOutlet var table2: UITableView! 

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! 
} 
相关问题