Two tables on same view controller not displaying data

I have 2 tableviews on the same view controller. All I am trying to do is get var text to be dispalyed on both of the tableviews right now nothing is being displayed on either of the tableviews.

         import UIKit

var text = ["a", "b"]

var row = 0

class ViewController: UIViewController,  UITableViewDelegate,  UITableViewDataSource {

    @IBOutlet var b: UITableView!
    @IBOutlet var a: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
}

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


           return 5
        }
        else {

            return 10
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == a {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            return cell
        }
        else {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "dx")
            return cell
            
        }
    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
row = indexPath.row
}}

Maybe you should do it like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell {
if tableView == a {
let cell = UITableViewCell(style: .default, reuseIdentifier: “cell”)
return cell
}
else if tableView == b {
let cell = UITableViewCell(style: .default, reuseIdentifier: “dx”)
return cell

    }
  return nil
}

override func viewDidLoad() {
super.viewDidLoad()
b.dataSource = self
b.delegate = self
…and same for table a
.
.
.
.

@timswift Thanks very much for your question! I believe @connyhakansson1’s solution should do the trick, but I do want to point out a few things here:

  1. Using two separate UITableViews on the screen is very poor practice. Neither your screen is big enough to support something like this, and if you’re trying to achieve different appearances for your UITableView, it is best to use multiple custom UITableViewCell’s, and/or sections. I honestly can’t think of a scenario where you would need to separate, unique tableViews on the SAME screen.

  2. UITableViews utilize the delegate pattern, which is a one to one relationship. What you’re doing by introducing ANOTHER UITableView for the same ViewController is blurring these lines, and thus, making your code more cluttered, and convoluted. This will make debugging much more difficult.

You’re doing the right thing by working with UITableViews in order to improve your iOS development skills, but it is best to learn good coding practices, and actually using them in real world scenarios. Scenarios like the one you’re stuck on are extremely rare, and don’t offer much learning potential.

Just my two cents :slight_smile:

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.