简体   繁体   English

如何在 swift 中的 tableview 单元格中显示 Api 响应

[英]How to show Api response in tableview cell in swift

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.我被困在我的代码中,我正在尝试向 API 响应 tableview 单元显示,但我不知道如何在数组中填充数据,所以没有在我的 tableviewcell 中显示任何内容。 I am using custome cell and Alamofire in swift.我正在快速使用客户单元和 Alamofire。 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编辑

在此处输入图片说明

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

If the API you're calling is well-made, you should use a get method, not a post.如果您调用的 API 制作精良,则应使用 get 方法,而不是 post。 Also, I tried to use " https://dousic.com/api/radiolist?user_id=16 " but it return另外,我尝试使用“ https://dousic.com/api/radiolist?user_id=16 ”但它返回

{
    "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.这两件事可能是你的问题,也可能是你的自定义单元格,或者你的 cellforrow 方法......如果你能显示更多的代码,它会有所帮助。


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.并尝试使用断点对其进行调试,以查看应用程序是否可以随心所欲,以及此时您的变量是什么。

If you are getting your array_RadioList from Api_call(), try this如果你从 Api_call() 得到你的 array_RadioList,试试这个

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.并检查 numberOfRowsInSection 函数。

Try this尝试这个

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return [self.array_RadioList].count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM