我很累,因为我觉得有一个简单的解决方案,但我好几天都找不到!我试图从 ViewController 中删除 parseJSON(),这样它看起来会更好,但删除后它就不起作用了,...
我很累,因为我觉得有一个简单的解决方案,但我好几天都找不到!我试图从 ViewController 中删除 parseJSON(),这样看起来会更好,但删除后它就不起作用了,但它仍在解析数据。在 ViewController 内部,这个函数运行得非常好,并显示带有解析数据的 tableView。我尝试添加 reloadData(),但没有帮助
https://i.sstatic.net/z5lgD.jpg - 截图
class MainViewController: UIViewController {
private var companyModel: CompanyModel?
private lazy var employeesTable: UITableView = {
let table = UITableView(frame: .zero, style: .grouped)
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
view.addSubview(employeesTable)
employeesTable.frame = view.bounds
employeesTable.delegate = self
employeesTable.dataSource = self
}
private func parseJSON() {
guard let url = URL(string: "https://run.mocky.io/v3/1d1cb4ec-73db-4762-8c4b-0b8aa3cecd4c") else { return }
do {
let jsonData = try Data(contentsOf: url)
companyModel = try JSONDecoder().decode(CompanyModel.self, from: jsonData)
} catch {
print(error)
}
}
正如您在第二张截图中看到的那样,它正在运行
import Foundation
class NetworkManager {
var companyModel: CompanyModel?
func parseJSON() {
guard let url = URL(string: "https://run.mocky.io/v3/1d1cb4ec-73db-4762-8c4b-0b8aa3cecd4c") else { return }
do {
let jsonData = try Data(contentsOf: url)
companyModel = try JSONDecoder().decode(CompanyModel.self, from: jsonData)
} catch {
print(error)
}
}
}
import UIKit
class MainViewController: UIViewController {
var networkManager = NetworkManager()
private var companyModel: CompanyModel?
private lazy var employeesTable: UITableView = {
let table = UITableView(frame: .zero, style: .grouped)
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
networkManager.parseJSON()
view.addSubview(employeesTable)
employeesTable.frame = view.bounds
employeesTable.delegate = self
employeesTable.dataSource = self
}
}
但这个不起作用,我做错了什么?
您的代码有两个严重问题
首先,我看到你有两个 companyModel
:第一个在类中 NetworkManager
,第二个在类中 MainViewController
,两者之间没有任何联系。当你解析 json 时,表视图会显示数据, MainViewController
因为它连接到 companyModel
in MainViewController
。你必须删除其中一个。
其次,当你从 URL 解析 JSON 时,你应该创建一个异步函数来解析 JSON NetworkManager
。有时从服务器获取数据可能需要很长时间。
代码如下
class NetworkManager {
func parseJSON(completion: @escaping(_ companyModel: CompanyModel?) -> Void) {
guard let url = URL(string: "https://run.mocky.io/v3/1d1cb4ec-73db-4762-8c4b-0b8aa3cecd4c") else { return }
do {
let jsonData = try Data(contentsOf: url)
let companyModel = try JSONDecoder().decode(CompanyModel.self, from: jsonData)
// return data parse from server
completion(companyModel)
} catch {
print(error)
}
}
}
class MainViewController: UIViewController {
var networkManager = NetworkManager()
private var companyModel: CompanyModel?
private lazy var employeesTable: UITableView = {
let table = UITableView(frame: .zero, style: .grouped)
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(employeesTable)
employeesTable.frame = view.bounds
employeesTable.delegate = self
employeesTable.dataSource = self
// add this line
self.initTableViewData()
}
func initTableViewData() {
networkManager.parseJSON(completion: {
companyModel in
self.companyModel = companyModel
self.employeesTable.reloadData()
})
}
}