UITableView in VC

Hey guys. I am trying to add UITableView do the View of my VC. On top of the VC I added to UIViews with the custom elements and they work just fine. However. When I am trying to add UITableView, it pushes UIViews from above out of the screen.



Code Block private func configureNoteTableView() {
        noteTableView = UITableView(frame: .zero)
        view.addSubview(noteTableView)
        noteTableView.layer.borderColor = UIColor.systemRed.cgColor
        noteTableView.layer.borderWidth = 5
        
        NSLayoutConstraint.activate([
            noteTableView.topAnchor.constraint(equalTo: addNoteView.bottomAnchor, constant: 0),
            noteTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            noteTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            noteTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
        ])
    }

This is how I configured it. addNoteView is the UIView above. What am I doing wrong?
Many thanks for help




Answered by jmgawe in 654022022
I fixed the issue by creating custom UIView that holds a UITableView inside.

However if anyone can explain why it did't work by simply laying out some constraints in a VC, I will be more than grateful

EDIT: It seems like I did not fix the issue with that at all. Table view does not populate and syntax error is the same
Accepted Answer
I fixed the issue by creating custom UIView that holds a UITableView inside.

However if anyone can explain why it did't work by simply laying out some constraints in a VC, I will be more than grateful

EDIT: It seems like I did not fix the issue with that at all. Table view does not populate and syntax error is the same
Hey guys. A little bit of clarification. The problem with the table View had a different nature which I solved. However, the error stayed, I have realised that its connected to these constraints:

Code Block NSLayoutConstraint.activate([
            addNoteTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
            addNoteTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
            addNoteTextField.widthAnchor.constraint(equalToConstant: 270),
            addNoteTextField.heightAnchor.constraint(equalToConstant: 50),
            
            addNoteButton.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
            addNoteButton.leadingAnchor.constraint(equalTo: addNoteTextField.trailingAnchor, constant: padding),
            addNoteButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
            addNoteButton.heightAnchor.constraint(equalToConstant: 50),
        ])


What could be possibly wrong with them?


Have you tried this before you add the tableview as a subview to your view:

noteTableView.translatesAutoresizingMaskIntoConstraints = false
Are they ALL the constraints you have defined ?
Code Block
addNoteTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
addNoteTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
addNoteTextField.widthAnchor.constraint(equalToConstant: 270),
addNoteTextField.heightAnchor.constraint(equalToConstant: 50),
addNoteButton.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
addNoteButton.leadingAnchor.constraint(equalTo: addNoteTextField.trailingAnchor, constant: padding),
addNoteButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
addNoteButton.heightAnchor.constraint(equalToConstant: 50),
])

What do you get ?
Does the tableView occupies all the screen height?
Or just move some other views ?

Please explain completely what you get, what you expect and what you did.

And show the complete code.
Did you set the delegate and dataSource for the TableView ?
UITableView in VC
 
 
Q