Coder
Coder

Reputation: 553

UITableView not showing Up in ViewController

I am building a project without storyboard. Everything is working fine but I can't seem to figure why I can't add tableView programmatically. I have tried the same code for adding tableView in another empty project and its working fine but inside my project the tableview is not showing up. My view hierarchy is like below.

I have a BaseClass like this:

class BaseController: UIViewController{
    override func viewDidLoad() {
        setupViews()
    }
       func setupViews(){

     }
}

Then I have firstViewController class inherited from Base Class:

class firstViewController: BaseController

Inside my firstController, I am declaring and initializing my tableView:

var tableView:UITableView = {
    let tbl = UITableView()
    tbl.translatesAutoresizingMaskIntoConstraints = false
    tbl.backgroundColor = .blue
    return tbl
}()

Then I am overriding setupView() inside firstViewController here like below:

override setupView() {
 
     view.addSubview(tableView)

     NSLayoutConstraint.activate([

        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        tableView.trailingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)

    ])  
 }

It should show empty tableView cells but it is not showing up. I guess there is something to do with the base and derive class thing but I can't figure out the exact problem.

Upvotes: 1

Views: 463

Answers (1)

Mahendra
Mahendra

Reputation: 8924

You need to set trailing constraint for tableview correctly.

tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0)

You were setting view.leading to tableview.trailing that makes your tableview invisible from current view.

Upvotes: 3

Related Questions