【问题标题】:How to show Api response in tableview cell in swift如何在表格视图单元格中快速显示 Api 响应
【发布时间】:2018-03-07 11:26:44
【问题描述】:

我卡在我的代码中,我正在尝试向 API 响应 tableview 单元格显示,但我不知道如何在数组中填充数据,因此在我的 tableviewcell 中没有显示任何内容。我正在迅速使用custome cell和Alamofire。请改进我的错误给我解决方案。

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

}`

感谢您的帮助。

编辑

【问题讨论】:

  • 我是 swift 新手,请您帮我解决我的问题@Moritz。
  • 这是我的 Api 响应 '{ 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" "dousic.com/uploads/s48937q.png"; "radio_tags" = ".977 Country"; "radio_title" = "Lee Brice - Love Like Crazy"; "radio_url"="7579.live.streamtheworld.com:3690/977_COUNTRY_SC"; },'
  • 请为您的 tableview 委托方法提供代码,并在此行 self.array_RadioList = resData as! [[String:AnyObject]] 之后打印 self.array_RadioList 来检查您的响应
  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell : homeCell = tableView.dequeueReusableCell(withIdentifier: "homeCell")!作为! homeCell var dict = array_RadioList[(indexPath as NSIndexPath).row] cell.lbl_name?.text = dict["radio_title"] as?字符串返回单元格 }

标签: ios swift uitableview alamofire


【解决方案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()
    }

【讨论】:

  • self.array_RadioList = resData as! [[String:Any]] 如何写这行它得到错误 Cast from'[JSON] to unrelated type '[[String:Any]]' 总是失败
  • 你为什么要用 [String:Any] 投射这个?。
  • 只需创建---- var array_RadioList: [JSON]?并将 resData 传递给 array_RadioList
  • 并在 cellForRowAt 中使用“let radio = array_RadioList?[indexPath.row]”来获取无线电对象。
  • func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array_RadioList! .count } 给出这个错误
【解决方案2】:

如果你从 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 }

并检查 numberOfRowsInSection 函数。

【讨论】:

  • 什么都不会改变。
  • 然后检查是否使用 tableview 函数中的断点调用 tableview 重新加载数据
【解决方案3】:

如果您调用的 API 制作精良,您应该使用 get 方法,而不是 post。 另外,我尝试使用“https://dousic.com/api/radiolist?user_id=16”但它返回了

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

这两件事可能是您的问题,也可能在您的自定义单元格中,或者在您的 cellforrow 方法中... 如果您可以显示更多代码,那将有所帮助。


编辑

尝试使用这个版本的可选链:

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

并尝试使用断点对其进行调试,以查看应用程序是否可以到达您想要的任何地方以及此时您的变量是什么。

【讨论】:

  • 不,我得到了回复。
  • get方法中如何使用参数。
  • { 响应 = { 代码 = 301; error = "错误的网址"; };使用 .GET 后出现此错误
  • 你知道如果你进入这个:if self.array_RadioList.count > 0 { self.tbl_home.reloadData() }
  • 显示 0 元素 .@Damien Bannerot
【解决方案4】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2015-04-16
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    相关资源
    最近更新 更多