Monday, 17 December 2018

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

I want to use Alamofire and SwiftyJSON for my REST API. I have accessed the root JSON but I can't access the objects of JSON.

This my json result:

[
    {
        "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"
            }
          ]
       }
    }

With this code:

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)")
        }
    }
}

My 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"] as? String
}

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

Please help me in viewing the datas in my tableView cells using Alamofire and SwiftyJSON. How can I access that data? I mean how can I get numberOfRowsInSection?



from Handling JSON with Alamofire & SwiftyJSON and Adding to UITableView in Swift

No comments:

Post a Comment