I have this here structured:
struct Excercise: Codable {
let excerciseID: String
let excerciseName: String
let description: String
let intensity: Int
}
Then, that's the var:
var excercistList = [Excercise]()
And the cellforrowat:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExcerciseCell", for: indexPath)
cell.textLabel?.text = self.excercistList[indexPath.row].excerciseName
return cell
}
That's the URLSession:
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else{return}
do {
let excerciseList = try JSONDecoder().decode(Excercise.self, from: data)
print(excerciseList.excerciseName)
} catch let jsonErr {
print("Error", jsonErr)
}
}.resume()
I get the right results on the print, but nothing on the table.
What am I doing wrong?
first of all you have to populate your dataSource
which is var excercistList = [Excercise]()
. After that you have to reload tableView
.
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else{return}
do {
let excerciseListFromDecoder = try JSONDecoder().decode(Excercise.self, from: data)
self.excercistList = excerciseListFromDecoder
self.tableView.reloadData()
} catch let jsonErr {
print("Error", jsonErr)
}
}.resume()
You have to be sure that you have set correctly
tableView.delegate = self
tableView.dataSource = self
and the numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.excercistList.count
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.