在一个ViewController Swift中使用两个UITableViews

时间:2018-10-22 07:53:31

标签: swift uitableview

如何在一个ViewController中创建两个UITableViews,我有一个问题

您需要每个人退货的问题不在此条件之内,并且我有每个Tableview的信息

此消息:“预期返回'Int'的函数中缺少返回值“

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

    if tableView == table_View {
    return list.count
    }

    if tableView == table_View2 {
    return list_2.count
    }
}

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

    if tableView == table_View {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
    cell.la_view.text = list[indexPath.row]
    cell.backgroundColor = UIColor(named: "Defeult")

    return cell
    }

    if tableView == table_View2 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell_2") as! TableView_Cell
    cell.la_view2.text = list_2[indexPath.row]
    cell.backgroundColor = UIColor(named: "Defeult")

    return cell
    }

}

4 个答案:

答案 0 :(得分:1)

问题是,例如在任何情况下,numberOfRowsInSection都必须返回某物。在您的实现中,您有两个if语句,但是您,但只有您知道这已经足够了,因为tableView只能是两个语句中的任何一个。不幸的是,编译器不知道这一点。因此,您可以通过一种简单的方式完成此操作:

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

    if tableView == table_View {
        return list.count
    }
    return list_2.count
}

注意:同样适用于cellForRowAt功能

也许更好:

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

    if tableView == table_View {
        return list.count
    } else if tableView == table_View2 {
        return list_2.count
    } 
    assertionFailure("Unexpected tableView")
    return 0
}

答案 1 :(得分:0)

numberOfRowsInSection内更改代码,因为这种委托方法至少需要一个Int值。由于您同时在if条件中使用两个值返回,因此在else情况下要求返回值的错误。 因此,每种情况都应返回一个Int值。

 if tableView == table_View {
    return list.count
    } 
  else {
    return list_2.count
    }

答案 2 :(得分:0)

作为一种快速解决方案(请确保阅读其余答案),您可以这样做:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (tableView == table_View) ? list.count : list_2.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == table_View {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
        cell.la_view.text = list[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }

    if tableView == table_View2 { {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_2") as! TableView_Cell
        cell.la_view2.text = list_2[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }

    return UITableViewCell()
}

重要提示:

请记住,如果必须在同一个视图控制器中添加两个表视图(应该不是一个很酷的主意),则可以分开来处理不同类中每个表视图的dataSource和委托(不是包含表视图的同一视图控制器)。示例:

在视图控制器中,将dataSource设置为:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let table_View = UITableView()
        let table_View2 = UITableView()

        table_View.dataSource = SourceHandler1()
        table_View2.dataSource = SourceHandler2()
    }
}

因此,实施:

class SourceHandler1: NSObject, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
        cell.la_view.text = list[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }
}

class SourceHandler2: NSObject, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        list_2.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_2") as! TableView_Cell
        cell.la_view2.text = list_2[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }
}

这可以避免出现“ 大量”视图控制器的问题,并减少产生Spaghetti code的可能性。

答案 3 :(得分:0)

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

    if tableView == table_View {
        return list.count
    }

    if tableView == table_View2 {
        return list_2.count
    }
    return 0    
}

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

    if tableView == table_View {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
        cell.la_view.text = list[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }

    if tableView == table_View2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_2") as! TableView_Cell
        cell.la_view2.text = list_2[indexPath.row]
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
    }
    return UITableViewCell()
}
相关问题