簡體   English   中英

使用Alamofire和SwiftyJSON處理JSON並在Swift中添加到UITableView

[英]Handling JSON with Alamofire & SwiftyJSON and Adding to UITableView in Swift

我想將Alamofire和SwiftyJSON用於我的REST API。 我已經訪問了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

嘗試這個:

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
}

第一件事是現在不要使用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
}

// 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!
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM