我在 postman 中有这个 json 响应...
{
"success": 1,
"order_data": [
{
"order_id": "00145",
"seller_id": "263",
"customer_id": "101",
"product_id": "8",
"product_name": "casserole",
"product_image": "http://myapp.com/public/uploads/products/123_45_6_image_123456789",
"product_quantity": "79",
"grand_total": "500",
"discount_price": "0",
"selling_price": "500",
"shipping_charges": "0",
"taxes": "0",
"applied_coupon_code": "",
"transaction_type": "COD",
"mobile_number": "9876543210",
现在必须在我的表格 View 中显示此数据的一些响应。作为第一步,我像这样在成功 block 中解析数据......
Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers)
.responseJSON { (response) in
if let httpResponse = response.response {
if httpResponse.statusCode == 200 {
if let result = response.result.value as? [String:Any] {
if result["success"] as! Int == 0 {
print("Something went wrong!")
} else if result["success"] as! Int == 1 {
if let orderData = result["order_data"] as? [[String:Any]] {
for order in orderData {
guard let orderNo = order["order_id"] as? String,
let status = order["order_status"] as? String,
let orderPlacedDate = order["created_at"] as? String,
let rate = order["selling_price"] as? String
else {continue}
//`Order` given below is a struct.
let theOrder = Order(order_id: orderNo, selling_price: rate, order_status: status, created_at: orderPlacedDate)
self.orderData.append(theOrder)
最后,我的 cellForRowAt
、numberOfRowsInSection
和 noOfItems
如下所示...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: OrdersTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ordersIdentifier") as! OrdersTableViewCell
let orderObj = orderData[indexPath.row]
cell.orderNo.text = orderObj.order_id
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return orderData.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
但是我的表格 View 显示为空白。我错过了什么..?希望有人能帮忙...
最佳答案
在viewDidLoad
中
self.tableView.dataSource = self
self.tableView.delegate = self
在orderData
中获取数据后
DispatchQueue.main.async {
self.tableView.reloadData()
}
//Instead of tableView use your tableView name
希望对你有帮助
关于ios - 从 json 在 tableview 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47532344/