Reputation: 1999
I need a simple UITableView
of 1 row and 6 columns, I'm trying to create a UITableView
programmatically and add it on my ViewController
but with no luck, I watched some tutorials and I don't really get what I'm doing wrong. Maybe if you guys take a look you could tell me.
This is my code:
class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mTableView: UITableView = {
//STACKOVERFLOW *In the first line, I'm just positioning it randomly to see if I can get the table view to show, will change later.*
var tableView = UITableView(frame: CGRect(x: 100, y: 100, width: 100, height: 100), style: .plain)
tableView.backgroundColor = .red
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(red: 80/255, green: 135/255, blue: 179/255, alpha: 1.0)
setupNavBar()
self.navigationItem.searchController = mSearchBarController
setupMainWeatherIcon()
setupTableView()
mTableView.dataSource = self
mTableView.delegate = self
}
private func setupTableView(){
self.view.addSubview(mTableView)
mTableView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
mTableView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
mTableView.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor).isActive = true
mTableView.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor).isActive = true
}
// MARK: TABLE VIEW METHODS!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "forecastCell", for: indexPath)
}
}
Upvotes: 0
Views: 49
Reputation: 91
You forgot to setup datasource for tableView
Add: mTableView.dataSource = self
UITableViewController add this automatically, UIViewController doesn't.
Also you forgot to setup height anchor... and Width. Or Top and Bottom Anchor
Upvotes: 1