【问题标题】:Display JSON data in UITableView with swift 4 and Alamofire [closed]使用 swift 4 和 Alamofire 在 UITableView 中显示 JSON 数据 [关闭]
【发布时间】:2018-06-20 06:33:09
【问题描述】:

我是 Swift 的新手,希望得到您的帮助。我正在做一个像这样快速解析 JSON 数据的应用程序: Project 这是我写的代码

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var tableView: UITableView!

var albumArray = [AnyObject]()
    var url = ("https://jsonplaceholder.typicode.com/photos")

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

                  Alamofire.request("https://jsonplaceholder.typicode.com/photos").responseJSON { (responseData) -> Void in
                if((responseData.result.value) != nil) {
                    let swiftyJsonVar = JSON(responseData.result.value!)
//
                    if let resData = swiftyJsonVar[].arrayObject {
                        self.albumArray = resData as [AnyObject]; ()

                    }
                    if self.albumArray.count > 0 {
                        self.tableView.reloadData()
                    }
                }
            }

        }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return albumArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CostumTableViewCell

        let title = albumArray[indexPath.row]
        cell?.titleLabel?.text = title["title"] as? String

        return cell!
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

问题是没有任何错误但不显示数据。您还可以看到 JSON:https://jsonplaceholder.typicode.com/photos

【问题讨论】:

  • 你能更准确地解释问题的“不起作用”部分吗?
  • @KeyurTailor 不显示数据。对不起
  • 你试过在控制台打印数据吗?
  • @KeyurTailor 是的,控制台中的数据打印成功
  • 那我建议你检查一下你收到的数据的格式。

标签: json swift alamofire swift4


【解决方案1】:

您的相同代码对我有用。在主队列中重新加载并确保为单元格中的标签设置约束。连接你的 tableview 的数据源和委托,然后检查。

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多