来自 API 的数据未显示在我的 UITableView 中
Data from API is not showing in my UITableView
我正在尝试将 API 中的数据显示到 UITableView 中。我遇到的问题是我没有获得任何进入 UITableView 的数据。现在我看到在我的数据源和委托中连接起来了。
我也知道我可以访问 API,因为我可以打印到控制台。
没有错误,只是我看不到任何数据。下面是我的代码。
class StandingTableViewController: UITableViewController {
var standing = ""
let SEASON_URL = "https://ergast.com/api/f1"
var champions: [DriverStanding] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.title = standing
fetchJSON(standing: standing)
}
private func fetchJSON(standing: String){
let JsonUrlString = SEASON_URL + "/" + String(standing) + "/driverstandings.json"
print(JsonUrlString)
guard let url = URL(string: JsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
DispatchQueue.main.async {
if let err = err {
print("Failed to get data from url:", err)
return
}
guard let data = data else { return }
do {
let decoder = JSONDecoder()
// Swift 4.1
decoder.keyDecodingStrategy = .convertFromSnakeCase
let firstDriver = try decoder.decode(F1Data.self, from: data)
self.champions = firstDriver.mrData.standingsTable.standingsLists[0].driverStandings
self.tableView.reloadData()
} catch {
print(error)
}
}
}.resume()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return champions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")
let champion = champions[indexPath.row]
let driverName = "\(champion.driver.givenName!) \(champion.driver.familyName!)"
cell.textLabel?.text = driverName
return cell
}
如有任何帮助,我们将不胜感激。我已检查 "cellId" 是否与我的代码中的匹配。
在numberOfSections
return1
或者删除整个方法为默认值是1
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
并重复使用单元格,在 Interface Builder 中设置字幕样式并写入
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
有这方面的更新吗?是否删除了方法:
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
工作?
我正在尝试将 API 中的数据显示到 UITableView 中。我遇到的问题是我没有获得任何进入 UITableView 的数据。现在我看到在我的数据源和委托中连接起来了。
没有错误,只是我看不到任何数据。下面是我的代码。
class StandingTableViewController: UITableViewController {
var standing = ""
let SEASON_URL = "https://ergast.com/api/f1"
var champions: [DriverStanding] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.title = standing
fetchJSON(standing: standing)
}
private func fetchJSON(standing: String){
let JsonUrlString = SEASON_URL + "/" + String(standing) + "/driverstandings.json"
print(JsonUrlString)
guard let url = URL(string: JsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
DispatchQueue.main.async {
if let err = err {
print("Failed to get data from url:", err)
return
}
guard let data = data else { return }
do {
let decoder = JSONDecoder()
// Swift 4.1
decoder.keyDecodingStrategy = .convertFromSnakeCase
let firstDriver = try decoder.decode(F1Data.self, from: data)
self.champions = firstDriver.mrData.standingsTable.standingsLists[0].driverStandings
self.tableView.reloadData()
} catch {
print(error)
}
}
}.resume()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return champions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")
let champion = champions[indexPath.row]
let driverName = "\(champion.driver.givenName!) \(champion.driver.familyName!)"
cell.textLabel?.text = driverName
return cell
}
如有任何帮助,我们将不胜感激。我已检查 "cellId" 是否与我的代码中的匹配。
在numberOfSections
return1
或者删除整个方法为默认值是1
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
并重复使用单元格,在 Interface Builder 中设置字幕样式并写入
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
有这方面的更新吗?是否删除了方法:
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
工作?