繁体   English   中英

在Swift 4中解析JSON后,为什么会得到空白的UITableView?

[英]Why am I getting a blank UITableView after parse JSON in Swift 4?

我不知道为什么单元格不返回数据。

我可以使用Decodable正常解析,这意味着它可以正常工作。

我一直在尝试所有发现的方法,但都没有成功。

struct MuscleGroup: Decodable {
   let ExcerciseID: String
   let description: String
   let excerciseName: String
   let muscleGroup: String
}


class ExerciseListViewController: UITableViewController {




var muscleGroup = [MuscleGroup]()


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return muscleGroup.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ExerciseList", for: indexPath) as! ExcerciseList

    let muscle = muscleGroup[indexPath.row]

    cell.textLabel!.text = muscle.excerciseName
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(self.muscleGroup[indexPath.row])
    tableView.deselectRow(at: indexPath, animated: true)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource = self
    self.tableView.delegate = self
    getJson()

}




func getJson(){

    guard let url = URL(string: "https://jsonurl") else { return }

    let session = URLSession.shared
    session.dataTask(with: url) { (data, response, error) in
        if let response = response {
            print(response)
        }

        if let data = data {
            print(data)
            do {
                let muscles = try JSONDecoder().decode([MuscleGroup].self, from: data)
                for muscle in muscles {


                    let muscleGroup = muscle.excerciseName
                    print(muscleGroup)
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }

                }
            } catch {
                print(error)
            }

        }
        }.resume()



}

如果将var muscleGroup = String更改为[“ Chest”,“ Back”,“ Abdominals”,“ Arms”,“ Legs”],它将正确返回。

另外,控制台上的打印结果将返回需要在“表视图”中的所有数据。

我究竟做错了什么?

在您的getJson函数中,此行

let muscleGroup = muscle.excerciseName

正在创建一个新的局部变量,名为muscleGroup ,将行更改为

self.muscleGroup.append(muscle.excerciseName)

即摆脱let appendappend到主数组变量

同时移动

 DispatchQueue.main.async {
     self.tableView.reloadData()
 }

到外面的用于循环muscles ,你正迫使表重新加载每个条目,而不是当你完成

因为您可能想使用整个结构

  1. 更换

     var muscleGroup = [String]() 

     var muscleGroups = [MuscleGroup]() 
  2. 更换

     let muscles = try JSONDecoder().decode([MuscleGroup].self, from: data) for muscle in muscles { let muscleGroup = muscle.excerciseName print(muscleGroup) DispatchQueue.main.async { self.tableView.reloadData() } } 

     self.muscleGroups = try JSONDecoder().decode([MuscleGroup].self, from: data) DispatchQueue.main.async { self.tableView.reloadData() } 
  3. 更换

     cell.textLabel?.text = self.muscleGroup[indexPath.row] 

     cell.textLabel?.text = self.muscleGroups[indexPath.row].muscleGroup 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM