現在我的ViewController代碼在這里--
import UIKit
struct jsonstruct。Codable {
let name: String[/span]。
let meta_data: [Categories]。
enum CodingKeys。String, CodingKey {
case name
case meta_data
}
}
struct Categories: Codable {
let value: String[/span]。
enum CodingKeys: String, CodingKey {
case value
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var arrdata : [jsonstruct] = [jsonstruct]()
var categorydata : [Categories] = [Categories] ()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource=self
getdata()
}
func getdata() {
let url = URL(string: "https://mywebstaging.net/ab/garnier/wp-json/wc/v3/products?consumer_key=<key>&consumer_secret=<secret>"/span>)
URLSession.shared.dataTask(with: url! ) { (data, response, error) in
do{if error == nil{
self.arrdata = try JSONDecoder() 。 decode([jsonstruct].self, from: data!)
print(self.arrdata)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch{
print("獲取json資料時出錯")
}
}.恢復()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1 {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView {
return arrdata.count
}else{
return categorydata.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableView {
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdata = arrdata[indexPath.row] 。
cell.lblid.text = getdata.name
return cell。
}
else{
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdatadesciption = self.categorydata[indexPath.row]
cell.lblname.text = getdatadesciption.value
return cell.
}
}
}
在表視圖中只顯示了 "名稱"。但 "值 "卻沒有出現。我得到的輸出是這樣的。請指導我。謝謝。

uj5u.com熱心網友回復:
替換
if tableView == tableView {
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdata = arrdata[indexPath.row] 。
cell.lblid.text = getdata.name
return cell。
}
else{
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdatadesciption = self.categorydata[indexPath.row]
cell.lblname.text = getdatadesciption.value
return cell.
}
隨著getdata.meta_data.first?.value包含頂級類別名稱
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdata = arrdata[indexPath.row] 。
cell.lblid.text = getdata.name
cell.lblname.text = getdata.meta_data.first?.value
return cell
你不需要維護2個陣列,它只有一個
。uj5u.com熱心網友回復:
替換
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView {
return arrdata.count
} else {
return categorydata.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableView {
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdata = arrdata[indexPath.row] 。
cell.lblid.text = getdata.name
return cell。
} else {
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let getdatadesciption = self.categorydata[indexPath.row]
cell.lblname.text = getdatadesciption.value
return cell.
}
}
With
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrdata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView. dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
let data = arrdata[indexPath.row] 。
let category = data.meta_data.first
cell.lblid.text = data.name
cell.lblname.text = category.value
return單元格
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311708.html
標籤:
