【发布时间】:2021-01-07 15:33:42
【问题描述】:
我正在尝试在 swift 5 中从 json 中的 php 文件中获取数据到我的 tableview 我能够将控制台中的数据作为 json 数组获取, 我想要的是将数据提取到我的自定义单元格中,但我没有获取 json 数据数组键,例如 data["user_id"] 它没有加载任何内容
func getUsers() {
AF.request(SiteUrl).validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let data = try JSON(data: json)
let str = data
print("DATA PARSED: \(str)")
}
catch{
print("JSON Error")
}
}
case .failure(let error):
print(error)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = "Hi Hadi"
//customCell.AdTitle.text =
return customCell
}
【问题讨论】:
-
我建议你改进你的问题,特别是尽量不要添加图片,看这里=>Why not upload images of code/errors when asking a question?
-
您似乎没有为您的资源分配任何内容
data -
解析 JSON 后,您需要重新加载 tableView。另外,避免使用
print("JSON Error"),而不是print("JSON Error: \(error)")。 -
不相关,但在 Swift5 中,使用 Codable 代替 SwiftyJSON 可能会更好,并且通常使用 Custom Struct 代替 Dict/array 进行 JSON 解析。