Ios 在同一视图控制器(swift3)中使用2个表视图

Ios 在同一视图控制器(swift3)中使用2个表视图,ios,uitableview,swift3,row,cell,Ios,Uitableview,Swift3,Row,Cell,我下面的代码是一个典型的tableview。我想做的是有两个tableview,它们各自的单元格cell和dx都显示var文本。我试着用“返回单元格和&cc”组合东西,但那不起作用。我只希望能够使用分隔两个不同TableView中的单元格,以便在同一视图控制器中显示相同的变量 import UIKit var text = ["a", "b"] var row = 0 class ViewController: UIViewController, UITableViewD

我下面的代码是一个典型的tableview。我想做的是有两个tableview,它们各自的单元格cell和dx都显示var文本。我试着用“返回单元格和&cc”组合东西,但那不起作用。我只希望能够使用分隔两个不同TableView中的单元格,以便在同一视图控制器中显示相同的变量

        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
}}

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!
}
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

    }
}

您可以使用两个tableview,如下所示:->

IBOutlet

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

UITableViewDataSource,UITableViewDelegate

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!
}
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

    }
}

我用你的建议编辑了代码。但是,var文本没有显示在任何一个文本视图上。您的代码建议反映在我上面编辑的问题中。