我在邮递员中有这个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",
现在,我的tableview中必须显示此数据的一些响应。作为第一步,我正在解析成功块中的数据,如此......
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
}
但我的桌面视图显示空白。我错过了什么..?希望有人能帮忙......
答案 0 :(得分:0)
在viewDidLoad
self.tableView.dataSource = self
self.tableView.delegate = self
在orderData
中获取数据后
DispatchQueue.main.async {
self.tableView.reloadData()
}
//Instead of tableView use your tableView name
希望这会对你有所帮助
答案 1 :(得分:0)
在追加来自API的reloadData
数组中的所有元素后,您必须调用orderData
,因为在附加数据之前需要一些时间来获取响应和tableView datasource
调用:
for order in orderData {
//....
}
self.YourTableView.reloadData()