简体   繁体   English

我如何在 swift 3 的一个视图控制器中填充两个不同的表视图

[英]How i can fill two different tableview in one viewcontroller in swift 3

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

    if tableView == table1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! acntTableViewCell
        cell.account.text = account[indexPath.row].email
        return cell
    }
    else if tableView == table2 {
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2")
            as! popTableViewCell

        cell2.pop.text = pop[indexPath.row].answer
        return cell2
    }
}//its give me  error here Missing return  in function

I am going to fill two different tables in one viewcontroller.我将在一个视图控制器中填充两个不同的表。 when I return cell it give me error Missing return in function where I am doing wrong can any one suggest me what's wrong with this code当我返回单元格时,它给我错误我做错的函数中缺少返回值,任何人都可以告诉我这段代码有什么问题

In the first place, you should compare tables using === (references), not == .首先,您应该使用=== (引用)而不是==来比较表。

This is one of the cases when an assertion failure is a good way to tell the compiler that no other way of the function exists eg:这是断言失败是告诉编译器不存在其他函数方式的好方法的情况之一,例如:

if tableView === table1 {
    return ...
} else if tableView === table2 {
    return ...
} else {
    fatalError("Invalid table")
}

You can also use a switch :您还可以使用switch

switch tableView {
   case table1:
       return ...
   case table2:
       return ...
   default:
       fatalError("Invalid table")
}

Both answers are correct, but I believe the best way to do it would be to separate each table view to have its own data source object, not a view controller.两个答案都是正确的,但我相信最好的方法是将每个表视图分开以拥有自己的数据源对象,而不是视图控制器。 Putting multiple tableview data source protocols adds a decent amount of unnecessary code, and if you refactor them into separate objects, you can help avoid a Massive View Controller.放置多个 tableview 数据源协议会增加大量不必要的代码,如果将它们重构为单独的对象,则可以帮助避免 Massive View Controller。

 class FirstTableViewDataSource : NSObject, UITableViewDataSource {

    var accounts: [ObjectTypeHere]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return accounts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AcntTableViewCell
        cell.account.text = accounts[indexPath.row].email
        return cell
    }

}

class SecondTableViewDataSource : NSObject, UITableViewDataSource {

    var pops: [ObjectTypeHere]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pops.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PopTableViewCell
        cell.account.text = pops[indexPath.row].answer
        return cell
    }
}

From there, just update the tableviews to pull from these objects从那里,只需更新 tableview 即可从这些对象中提取

override func viewDidLoad() {
    super.viewDidLoad()
    self.table1.dataSource = FirstTableViewDataSource()
    self.table2.dataSource = SecondTableViewDataSource()
}

The compiler is analyzing what will happen if tableView is neither table1 nor table2 .编译器正在分析如果tableView既不是table1也不是table2会发生什么。 If that should happen, the function will exit with nothing to return.如果发生这种情况,该函数将退出,没有任何返回。

That's an error.那是一个错误。

Your cellForRowAt method should always return a cell, so你的cellForRowAt方法应该总是返回一个单元格,所以

Try this way试试这个方法

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

    if tableView == table1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! acntTableViewCell
        cell.account.text = account[indexPath.row].email
        return cell
    }
     //if tableView is not table1 then
    let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2")
            as! popTableViewCell

     cell2.pop.text = pop[indexPath.row].answer
     return cell2
}
import UIKit

class ViewController: UIViewController {
@IBOutlet weak var table1: UITableView!
@IBOutlet weak var table2: UITableView!
let firstClassRef = FirstTableView()
let secondClassRef = SecondTableView()
override func viewDidLoad() {
    super.viewDidLoad()

   firstClassRef.array1 = ["1","2","3"]
    secondClassRef.array2 = ["1","2","3","1","2","3"]

    self.table1.dataSource = firstClassRef
    self.table2.dataSource = secondClassRef

    self.table1.delegate = firstClassRef
    self.table2.delegate = secondClassRef

    self.table1.reloadData()
    self.table2.reloadData()
 }
}

class FirstTableView: NSObject, UITableViewDataSource, UITableViewDelegate {
var array1 = Array<Any>()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return array1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as UITableViewCell
    cell.textLabel?.text = array1[indexPath.row] as? String
    cell.backgroundColor = UIColor.yellow
    return cell
}
}

class SecondTableView: NSObject, UITableViewDataSource, UITableViewDelegate {
var array2 = Array<Any>()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return array2.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as UITableViewCell
    cell.textLabel?.text = array2[indexPath.row] as? String
    cell.backgroundColor = UIColor.yellow
    return cell
}
}

Use Switch Statement使用 Switch 语句

import UIKit

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {
    @IBOutlet weak var topTableView: UITableView!
    @IBOutlet weak var downTableview: UITableView!
    var topData : [String] = []
    var downData = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        topTableView.delegate = self
        downTableview.delegate = self
        topTableView.dataSource = self
        downTableview.dataSource = self

        for index in 0...20 {
            topData.append("Top Table Row \(index)")
        }

        for index in 10...45 {
            downData.append("Down Table Row \(index)")
        }

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var numberOfRow = 1
        switch tableView {
        case topTableView:
            numberOfRow = topData.count
        case downTableview:
            numberOfRow = downData.count
        default:
            print("Some things Wrong!!")
        }
        return numberOfRow
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        switch tableView {
        case topTableView:
            cell = tableView.dequeueReusableCell(withIdentifier: "topCell", for: indexPath)
            cell.textLabel?.text = topData[indexPath.row]
            cell.backgroundColor = UIColor.green
        case downTableview:
            cell = tableView.dequeueReusableCell(withIdentifier: "downCell", for: indexPath)
            cell.textLabel?.text = downData[indexPath.row]
            cell.backgroundColor = UIColor.yellow
        default:
            print("Some things Wrong!!")
        }
        return cell
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 如何在Swift中从其他ViewController重新加载TableView中的数据 - How to reload data in a TableView from a different ViewController in Swift 如何使用tableview重新加载以下viewcontroller? - How can I reload the following viewcontroller with a tableview? iOS Swift如何从其他控制器重新加载tableView - IOS Swift how can I reload a tableView from a different controller 如何将两个委托放到一个视图控制器 - How can I put two delegate to one viewcontroller 如何在一个 ViewController 中使用两个 Webview? - how can I use two Webviews in one ViewController? 如何用另一个ViewController的数据填充TableView - How to fill TableView with data from another ViewController 无法将TableView @IBOutlet连接到Swift 2中的ViewController - Can't connect TableView @IBOutlet to ViewController in Swift 2 一个ViewController中可以有两个UISearchBar吗? - Can I have two UISearchBar in one ViewController? swift-如何检查特定的viewController是否为先前的viewController - swift - how can i check if a specific viewController is the previous viewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM