Я добавил TableView в построителе интерфейса, а затем принял протоколы UITableViewDelegate и UITableViewDataSource в View Controller. Я реализовал 3 метода, показанных ниже, как того требуют протоколы.
Моя цель - заполнить табличное представление данными из массива в файле модели. Однако ни один из 3 методов никогда не вызывается (я использовал операторы print, чтобы проверить это).
В чем проблема?
import UIKit
class CoinViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var coinList = CoinList()
func numberOfSections(in tableView: UITableView) -> Int {
print("1")
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
print("21")
return coinList.coins.count
} else{
print("22")
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "coinCell", for: indexPath)
let coin = coinList.coins[indexPath.row]
cell.textLabel?.text = coin.name
cell.detailTextLabel?.text = coin.value
print("3")
return cell
}
}