MVC
ViewController
class ViewController: UIViewController {
private var musicData: [Music]?
private let networkManager = NetworkManager.shared
private let tableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
setTableView()
fetchData()
}
private func setTableView() {
view.addSubview(tableView)
tableView.frame = view.bounds
tableView.dataSource = self
}
private func fetchData() {
networkManager.requestData(term: "jazz") { result in
switch result {
case .success(let data):
self.musicData = data
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
musicData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = musicData?[indexPath.row].musicTitle
return cell
}
}
Last updated