Multiple Table Views

Hi, I have a table view and am trying to implement a second, here is the code for the second one. The problem is the second one won't show any data. Please note that they're identical code:

Code Block
import UIKit
import GoogleMobileAds
class Home: UIViewController, GADBannerViewDelegate, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var bannerView: GADBannerView!
    @IBOutlet weak var ShowAds: UILabel!
    @IBOutlet weak var bannerViewHeight: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var Welcome: UILabel!
    var names = [
        Utilities.mostRecent
    ]  
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        checkRecentCat()
        Utilities.checkads(bannerView: bannerView, bannerViewHeight: bannerViewHeight)
        if Utilities.openNum == 1 {
            Welcome.text = "Welcome! Check out our app. In the categories section you will find all of our jokes!"
        }
      bannerView.rootViewController = self
        bannerView.delegate = self
        if Utilities.ShowAds == true {
            ShowAds.text = "Show Ads: True"
        }else if Utilities.ShowAds == false {
            ShowAds.text = "Show Ads: False"
        }
      }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        checkRecentCat()
        if UserDefaults.standard.bool(forKey: "JokesRUs.RemoveAds") {
            Utilities.ShowAds = false
            bannerViewHeight.constant = 0
            ShowAds.text = "Show Ads: False"
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("index path: \(indexPath.row)")
        let row = indexPath.row
        if row == 0 {
            self.performSegue(withIdentifier: Utilities.mostRecentSegue!, sender: self)
        }else if row == 1 {
            self.performSegue(withIdentifier: Utilities.secondRecentSegue!, sender: self)
        }else if row == 2 {
            self.performSegue(withIdentifier: Utilities.thirdRecentSegue!, sender: self)
        }
        tableView.deselectRow(at: indexPath, animated: true)}
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)      
        cell.textLabel?.text = names[indexPath.row]       
        return cell
    }
    func checkRecentCat() {
        print("Most Recent Cat: \(Utilities.mostRecent ?? nil)")
        print("Second Most Recent Cat: \(Utilities.secondRecent ?? nil)")
        if Utilities.secondRecent == nil || Utilities.secondRecentSegue == nil || Utilities.secondRecent == "Please choose a category to show" || Utilities.secondRecentSegue == "Please choose a category to show" {
            Utilities.setSecondRecent(id: "Please choose a category to show", segue: "categories")
        }
        if Utilities.thirdRecent == nil || Utilities.thirdRecentSegue == nil || Utilities.thirdRecent == "Please choose a category to show" || Utilities.thirdRecentSegue == "Please choose a category to show" {
            Utilities.setThirdRecent(id: "Please choose a category to show", segue: "categories")
        }
        if Utilities.mostRecent == nil || Utilities.mostRecentSegue == nil || Utilities.mostRecent == "Please choose a category to show" || Utilities.mostRecentSegue == "Please choose a category to show" {
            Utilities.setMostRecent(id: "Please choose a category to show", segue: "categories")
            tableView.isHidden = true
        }else {
            names = [Utilities.mostRecent]
            if Utilities.secondRecent == nil || Utilities.secondRecentSegue == nil || Utilities.secondRecent == "Please choose a category to show" {
            }else {
                names = [Utilities.mostRecent, Utilities.secondRecent]
                if Utilities.thirdRecent == nil || Utilities.thirdRecentSegue == nil || Utilities.thirdRecent == "Please choose a category to show" {
                }else {
                    names = [Utilities.mostRecent, Utilities.secondRecent, Utilities.thirdRecent]
                }
            }
        }
    }


Answered by Claude31 in 667172022
Where is the other tableView ? In another ViewController ?
I assume it is in another VC.

have you checked that names is not empty ?
You can test in viewDidLoad by adding
Code Block
print("number of names", names.count)

Have you defined dataSource and delegate for the tableView ?
You should do it directly in IB (as tableView is declared as IBOutlet) or in code.
In this case, add in viewDidLoad:
Code Block
tableView.delegate = self
tableView.dataSource = self

Notes:
  • you should give a more significant name to tableView. Such as namesTableView.

To do so, right click on tableView in
Code Block
    @IBOutlet weak var tableView: UITableView!
and select Refactor>Rename
enter the new name (it will change everywhere, including in IB which is essential)
  • code is hard to read lines 52/53 or 32 or 64.

Code Block
tableView.deselectRow(at: indexPath, animated: true)}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}

You should mark more clearly the separation between func with an extra line:
Code Block
//
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}



Accepted Answer
Where is the other tableView ? In another ViewController ?
I assume it is in another VC.

have you checked that names is not empty ?
You can test in viewDidLoad by adding
Code Block
print("number of names", names.count)

Have you defined dataSource and delegate for the tableView ?
You should do it directly in IB (as tableView is declared as IBOutlet) or in code.
In this case, add in viewDidLoad:
Code Block
tableView.delegate = self
tableView.dataSource = self

Notes:
  • you should give a more significant name to tableView. Such as namesTableView.

To do so, right click on tableView in
Code Block
    @IBOutlet weak var tableView: UITableView!
and select Refactor>Rename
enter the new name (it will change everywhere, including in IB which is essential)
  • code is hard to read lines 52/53 or 32 or 64.

Code Block
tableView.deselectRow(at: indexPath, animated: true)}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}

You should mark more clearly the separation between func with an extra line:
Code Block
//
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}



Thank you! The second thing worked
Multiple Table Views
 
 
Q