-1

I am stuck in my code, I am trying show to API response tableview cell but i have not any idea how to fill data in array ,So not showing anything in my tableviewcell. I am using custome cell and Alamofire in swift. Please improve my mistake give me solution .

func Api_call()
{
    let url = URL(string: "https://dousic.com/api/radiolist")!
    let components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
    // let fragment = components.fragment!
    print(components)

    let params = ["user_id":"16"  ]

    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default).responseJSON {response in
         self.hideActivityIndicator()
        var err:Error?
        switch response.result {
        case .success(let value):
            print(value)


            let json = JSON(value)

            // returns nil if it's not an array
            if let resData = json["radioList"].arrayObject
            {
                self.array_RadioList = resData as! [[String:AnyObject]]
            }
              if self.array_RadioList.count > 0 {
                    self.tbl_home.reloadData()
                }

        case .failure(let error):
            err = error
            print(err ?? "error .....")
        }
    }

}`

Thanks for help .

EDIT

enter image description here

Pooja Srivastava
  • 721
  • 1
  • 6
  • 19
  • I am new in swift so can you please help me to resolve my problem @ Moritz. – Pooja Srivastava Sep 26 '17 at 09:51
  • this is my Api response '{ response = { code = 200; radioList = ( { "genre_name" = ( { "genre_id" = 1; "genre_name" = Country; } ); "radio_des" = "Lee Brice - Love Like Crazy "; "radio_fev" = 1; "radio_id" = 30; "radio_img" "https://dousic.com/uploads/s48937q.png"; "radio_tags" = ".977 Country"; "radio_title" = "Lee Brice - Love Like Crazy"; "radio_url"="http://7579.live.streamtheworld.com:3690/977_COUNTRY_SC"; },' – Pooja Srivastava Sep 26 '17 at 09:57
  • Please provide code for your tableview delegate methods, also check your response by printing self.array_RadioList after this line `self.array_RadioList = resData as! [[String:AnyObject]]` – Anand Yadav Sep 26 '17 at 10:03
  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell : homeCell = tableView.dequeueReusableCell(withIdentifier: "homeCell")! as! homeCell var dict = array_RadioList[(indexPath as NSIndexPath).row] cell.lbl_name?.text = dict["radio_title"] as? String return cell } – Pooja Srivastava Sep 26 '17 at 10:06
  • Possible duplicate of [How to parse JSON response from Alamofire API in Swift?](https://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift) – Satish Babariya Sep 26 '17 at 10:42

4 Answers4

1
Just create a radio list variable like this 

    var array_RadioList:[JSON]?

Get array from json like this    
-
    if let resData = json["response"]["radioList"].array {
                    self.array_RadioList = resData
                    self.tableView.reloadData()
                }
and reload data.And get radio object in 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell? = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)
        let radio:JSON? = array_RadioList?[indexPath.row]
        cell?.textLabel?.text = radio?["radio_tags"].string
        return cell ?? UITableViewCell()
    }
Pardeep Bishnoi
  • 161
  • 1
  • 7
0

If the API you're calling is well-made, you should use a get method, not a post. Also, I tried to use "https://dousic.com/api/radiolist?user_id=16" but it return

{
    "response": {
        "code": "301",
        "error": "wrong url"
    }
}

These 2 things could be your problem, or it could be in your custom cells, or in you cellforrow method... If you can show more code it would help.


EDIT

Try to use this version of the optional chaining :

if let resData = json["radioList"].arrayObject as? [[String:AnyObject] {
    self.array_RadioList = resData
    self.tbl_home.reloadData()
}

and try to debug it with breakpoints to see if the application goes everywhere you want and what are your variables at this time.

Damien
  • 3,322
  • 3
  • 19
  • 29
0

If you are getting your array_RadioList from Api_call(), try this

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell : homeCell = tableView.dequeueReusableCell(withIdentifier: "homeCell")! as! homeCell cell.lbl_name?.text = array_RadioList[indexPath.row]["radio_title"] as? String return cell }

and also check for numberOfRowsInSection function.

Anand Yadav
  • 479
  • 5
  • 17
0

Try this

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return [self.array_RadioList].count;
}
Pooja Srivastava
  • 721
  • 1
  • 6
  • 19