-1

I am trying to have two tableviews in a single view, i have given a unique identifier for each cell and each tableview has their own cell class. The codes are

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

However, it keeps crashing as says my cell isnt registered? May i know what mistake i did here? I have also linked the tableview delegates to self

rmaddy
  • 314,917
  • 42
  • 532
  • 579
William Loke
  • 377
  • 1
  • 4
  • 25
  • 1
    Don't add the "2" like that, that's not how works Delegates. Instead, in each method, do `if tableView == self.tableView2{ //It's self.tableView2} else { //It's self.tableView }` (the tested `tableView` being the parameter of the method – Larme Jun 11 '18 at 16:18
  • this will show both of my tableview? – William Loke Jun 11 '18 at 16:18
  • Yes, that's in Objective-C https://stackoverflow.com/questions/6519673/ios-tableview-delegate-methods-for-two-tableview but the logic is the same. – Larme Jun 11 '18 at 16:19
  • Ok, thank you so much for the help! – William Loke Jun 11 '18 at 16:32

1 Answers1

3

You need to switch all methodfs like this

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

    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Oh, this will show both my tableview and their cells? – William Loke Jun 11 '18 at 16:19
  • does this goes the same for height, didselectrow and etc for the rest? – William Loke Jun 11 '18 at 16:20
  • thank you! it worked. May i know, why would we declare as if tableView == self.tableview? – William Loke Jun 11 '18 at 16:24
  • this data source method is called for both the tableViews so you need to know which cell you should return for anyone so you have to check the current tableView this method is called for , your crash happened because other tableView called and you dequeued a cell that wasn't registered for it – Shehata Gamal Jun 11 '18 at 16:28