Recently a few things might have changed in iOS development. Instead of AppDelegate's didFinishLauching now we have to set up our window and root VC in SceneDelegate. (No storyboard ,please!)OK, that is not so difficult, and not so different. But now it looks I can't even dequeue cells on my UITableViewDatasource now. Something as simple as that! What am I doing wrong again? I set the number of rows to 80 just to make sure, something will appear, but no luck there. I get the systemTeal colored tableView inside my ViewController (embedded in a navigationController), and that' it. Any Ideas?
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let cellId = "cellId"
lazy var tableView: UITableView = {
let tb = UITableView()
tb.translatesAutoresizingMaskIntoConstraints = false
return tb
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemPink
setupTableView()
}
fileprivate func setupTableView() {
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
tableView.backgroundColor = .systemTeal
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tableView.widthAnchor.constraint(equalTo: view.widthAnchor),
tableView.heightAnchor.constraint(equalTo: view.heightAnchor)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 80 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: cellId)!
cell.backgroundColor = .systemRed
cell.textLabel?.text = "cell: \(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 50 }
func numberOfSections(in tableView: UITableView) -> Int { 1 }
}
Besides setting the delegate, you should also set the data source:
tableView.dataSource = self