ios - 从 json 在 tableview 中显示数据

标签 ios swift uitableview

我在 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)

最后,我的 cellForRowAtnumberOfRowsInSectionnoOfItems 如下所示...

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/

相关文章:

ios - NSJSONSerialization 在验证应用程序收据时导致 Swift 崩溃

objective-c - iOS:使用@private 隐藏属性不被设置

ios - 根据iPhone屏幕大小显示不同的xib文件

ios - 当应用程序在后台时检索当前位置

ios - Ios 中 TableView 中的动画

ios - 从一个 ViewController 到另一个 ViewController 保存和删除 NSUserDefaults - Swift 2

ios - 我们在 ARC 中创建对象的这两种方式有什么区别吗?

ios - UITableViewCell 没有正确更新 swift 2.2

ios - 如何指定在 UITableView 中显示哪些单元格?

ios - 为什么 UITableViewCell 背景颜色不起作用(在界面生成器中设置)?