【问题标题】:use 2 tableviews in the same view controller (swift3)在同一个视图控制器中使用 2 个表视图(swift 3)
【发布时间】:2017-09-20 04:50:12
【问题描述】:

我下面的代码是一个典型的表格视图。我想做的是有两个表格视图,其中各自的单元格 cell 和 dx 都显示 var 文本。我尝试使用“return cell && cc”组合事物,但这不起作用。我只想能够用来分隔 2 个不同的表视图中的单元格,以在同一个视图控制器中显示相同的变量。

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

【问题讨论】:

    标签: ios uitableview swift3 row cell


    【解决方案1】:

    你可以使用如下两个tableview:->

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

    【讨论】:

    • 我根据您的建议编辑了代码。但是 var 文本没有显示在任何一个文本视图上。
    • 您的代码建议反映在我上面编辑的问题中。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多