Ios 一个视图中有两个表视图

Ios 一个视图中有两个表视图,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在一个视图中有两个tableview,我为每个单元格提供了一个唯一的标识符,每个tableview都有自己的单元格类。代码是 class ReportsViewController: UIViewController { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var tableView2: UITableView! override func viewDidLoad() { super.viewDid

我试图在一个视图中有两个tableview,我为每个单元格提供了一个唯一的标识符,每个tableview都有自己的单元格类。代码是

class ReportsViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableView2: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
 }
}

extension ReportsViewController : UITableViewDelegate, UITableViewDataSource {

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

    return 1
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell

        return cell

}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 145
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Breaking News"
}

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .medium
    let date = Date()
    let dateString = dateFormatter.string(from: date)
    return "Updated on " + dateString
}

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

    return 2
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TopStoriesTableViewCell

    return cell

}


private func tableView2(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 145
}

private func tableView2(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

private func tableView2(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return " Top Stories"
}

private func tableView2(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .medium
    let date = Date()
    let dateString = dateFormatter.string(from: date)
    return "Updated on " + dateString
}
}

但是,它不断崩溃,说我的手机没有注册?我能知道我在这里犯了什么错误吗?我还将tableview代理链接到self

您需要像这样切换所有方法

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

    if tableView  == self.tableView {

         let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell

         return cell
    }
    else {

          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! cell2

          return cell

    }
}

不要像那样添加“2”,这不是代理的工作方式。相反,在每个方法中,如果tableView==self.tableView2{//It's self.tableView2}或者{//It's self.tableView}(测试的
tableView
是方法的参数,这将显示我的两个tableView?是的,这在Objective-C中,但逻辑是相同的。好的,非常感谢您的帮助!哦,这将显示我的tableView和它们的单元格?高度、didselectrow等是否相同?谢谢!它起作用了。我可以知道吗,为什么我们要声明为tableView==self.tableView?这个数据源方法是为两个tableView调用的,所以你需要知道你应该为任何人返回哪个单元格,所以你必须检查这个方法调用的当前tableView,你的崩溃是因为另一个tableView调用了,而你将一个没有为我注册的单元格出列T