Conformed the LocationsListViewController view controller to the UITableViewDataSource and the UITableViewDelegate protocols.

This commit is contained in:
Javier Cicchelli 2023-04-12 16:37:46 +02:00
parent ea9fea98b3
commit 51e4c64a11

View File

@ -25,6 +25,8 @@ class LocationsListViewController: BaseViewController {
private lazy var table = {
let table = UITableView(frame: .zero, style: .plain)
table.dataSource = self
table.delegate = self
table.translatesAutoresizingMaskIntoConstraints = false
return table
@ -50,10 +52,47 @@ class LocationsListViewController: BaseViewController {
setupBar()
setupView()
bindViewModel()
viewModel.loadLocations()
}
}
// MARK: - UITableViewDataSource
extension LocationsListViewController: UITableViewDataSource {
// MARK: Functions
func numberOfSections(in tableView: UITableView) -> Int {
viewModel.numberOfSectionsInData
}
func tableView(
_ tableView: UITableView,
numberOfRowsInSection section: Int
) -> Int {
viewModel.numberOfDataItems(in: section)
}
func tableView(
_ tableView: UITableView,
cellForRowAt indexPath: IndexPath
) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
let entity = viewModel.dataItem(at: indexPath)
cell.textLabel?.text = entity.name
return cell
}
}
// MARK: - UITableViewDelegate
extension LocationsListViewController: UITableViewDelegate {}
// MARK: - Helpers
private extension LocationsListViewController {
@ -76,7 +115,13 @@ private extension LocationsListViewController {
view.addSubview(table)
view.addSubview(error)
view.addSubview(loading)
error.onRetry = { print("RETRY BUTTON PRESSED!") }
error.onRetry = {
self.viewModel.loadLocations()
}
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
NSLayoutConstraint.activate([
error.widthAnchor.constraint(equalToConstant: 300),
view.centerXAnchor.constraint(equalTo: error.centerXAnchor),
@ -99,6 +144,10 @@ private extension LocationsListViewController {
self.error.isHidden = viewStatus != .error
self.loading.isHidden = viewStatus != .loading
self.table.isHidden = viewStatus != .loaded
if viewStatus == .loaded {
self.table.reloadData()
}
}
.store(in: &cancellables)
}