【问题标题】:Handling JSON with Alamofire & SwiftyJSON and Adding to UITableView in Swift使用 Alamofire 和 SwiftyJSON 处理 JSON 并在 Swift 中添加到 UITableView
【发布时间】:2019-04-14 19:34:06
【问题描述】:

我想为我的 REST API 使用 Alamofire 和 SwiftyJSON。我已经访问了根 JSON,但我无法访问 JSON 的对象。

这是我的 json 结果:

[
    {
        "ID": 1,
        "name": "JABODETABEK",
        "cabang": [
            {
                "ID": 1,
                "wilayah_id": 1,
                "name": "Jembatan Lima"
            },
            {
                "ID": 2,
                "wilayah_id": 1,
                "name": "Kebon Jeruk"
            }
        ]
    },
    {
        "ID": 2,
        "name": "Sumatra Selatan dan Bangka Belitung",
        "cabang": [
            {
                "ID": 6,
                "wilayah_id": 2,
                "name": "A. Yani - Palembang"
            },
            {
                "ID": 7,
                "wilayah_id": 2,
                "name": "Veteran - Palembang"
            }
          ]
       }
    }

使用此代码:

Alamofire.request(url).responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let swiftyJsonVar = JSON(responseData.result.value!)

        if let resData = swiftyJsonVar.arrayObject {
            self.productArray = resData as! [[String:AnyObject]]
            print("MyArray: \(self.productArray)")
        }
    }
}

我的表格视图:

func numberOfSections(in tableView: UITableView) -> Int {
    return self.productArray.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let dic = productArray[section]
    return dic["name"] as? String
}

func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
    return (((?)))
}

请帮助我使用 Alamofire 和 SwiftyJSON 查看我的 tableView 单元格中的数据。我怎样才能访问这些数据?我的意思是我怎样才能得到numberOfRowsInSection

【问题讨论】:

  • productArray[section]["cabang"].count
  • 在哪里重新加载表格视图?是否在主线程中调用了 reload?

标签: ios json swift uitableview swifty-json


【解决方案1】:

//MARK:- UITableView 委托和数据源

func numberOfSections(in tableView: UITableView) -> Int {

    return self.productArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let dictionaryProduct = self.productArray.object(at: section) as! NSDictionary
    let arrayCabang = dictionaryProduct.object(forKey: "cabang") as! NSArray
    if arrayCabang.count > 0 {

        return arrayCabang.count
    } else {

        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let dictionaryProduct = self.productArray.object(at: indexPath.section) as! NSDictionary
    let arrayCabang = dictionaryProduct.object(forKey: "cabang") as! NSArray
    if arrayCabang.count > 0 {

        let dictionaryCurrentCabang = arrayCabang.object(at: indexPath.row) as! NSDictionary

        //Here you get data of cabangs at the particular index
    }

    return cell!
}

【讨论】:

    【解决方案2】:

    试试这个:

    Alamofire.request("url").responseJSON { (responseData) -> Void in
        if let data = response.data {
            guard let json = try? JSON(data: data) else { return }
            self.productArray = json.arrayValue //productArray type must be [[String:AnyObject]]   
        }
    

    之后更新 tableView 委托函数:

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.productArray.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section:   Int) -> String? {
        let dic = productArray[section]
        return dic["name"].string
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
        return productArray[sectionInd]["cabang"].arrayValue.count
    }
    

    【讨论】:

      【解决方案3】:

      第一件事是现在不要使用 Dictionary 或 Dictionary 数组。你可以用 Codable 代替 https://medium.com/@multidots/essentials-of-codable-protocol-in-swift-4-c795a645c3e1,

      如果您使用字典,则使用 Any 而不是 AnyObject。

      你的答案是(self.productArray[sectionInd]["cabang"] as! [[String:Any]]).count

      func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
      
          return ((self.productArray[sectionInd]["cabang"] as! [[String:Any]]).count
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-27
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        相关资源
        最近更新 更多