【发布时间】:2019-07-01 01:58:31
【问题描述】:
我这里有这样的结构:
struct Excercise: Codable {
let excerciseID: String
let excerciseName: String
let description: String
let intensity: Int
}
那么,这就是 var:
var excercistList = [Excercise]()
还有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
}
这就是 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()
我在打印上得到了正确的结果,但在桌面上什么都没有。
我做错了什么?
【问题讨论】:
-
数组是否填充了您想要的数据?
print(excerciseList.excerciseName)numberOfRowsInSection 方法。 -
填充数组后,您的表中需要一个 reloadData。
-
你拿到数组后有没有给
tableView.reloadData()打电话? -
reloadData 可以帮到你。
标签: ios json uitableview swift4