【问题标题】:Why am I getting a blank UITableView after parse JSON in Swift 4?为什么我在 Swift 4 中解析 JSON 后得到一个空白的 UITableView?
【发布时间】:2019-06-27 15:04:14
【问题描述】:

我不明白为什么单元格没有返回数据。

我可以使用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"] 它会正确返回。

另外,控制台上的打印结果会返回所有需要在 Table View 上的数据。

我做错了什么?

【问题讨论】:

    标签: ios json swift iphone uitableview


    【解决方案1】:

    在你的 getJson 函数中这一行

    let muscleGroup = muscle.excerciseName
    

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

    self.muscleGroup.append(muscle.excerciseName)
    

    即去掉letappend主数组变量的值

    同时移动

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

    要在 muscles 的 for 循环之外,因为您要强制表为每个条目重新加载,而不是在完成时重新加载

    【讨论】:

      【解决方案2】:

      你可能想使用整个结构

      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
        

      【讨论】:

      • 就像一个魅力。非常感谢你,先生。我现在会试着理解这一点。感激不尽,
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2022-10-05
      • 2012-05-16
      相关资源
      最近更新 更多