简体   繁体   English

使用SwiftyJSON和Alamofire的TableView上的JSON数据

[英]JSON data on TableView using SwiftyJSON and Alamofire

I want to show data on a Table View. 我想在表视图上显示数据。 The problem is, I can't get it to show up on it. 问题是,我无法显示它。 My code is shown below and I hope somebody can help me out. 我的代码如下所示,希望有人能帮助我。

import Foundation

struct HeroStats {

  let localized_name: String
  let primary_attr: String
  let attack_type: String
  let legs: String
  let img: String
}

Here is my viewController code: 这是我的viewController代码:

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

  @IBOutlet var tableView: UITableView!
  var heros = [HeroStats]()

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

    downloadJson()
    self.tableView.reloadData()


    tableView.delegate = self
    tableView.dataSource = self

  }

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

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.textLabel?.text = 
    heros[indexPath.row].localized_name.capitalized
    return cell
  }

  //MARK: Parsing JSON data
  func downloadJson(){
Alamofire.request("https://api.opendota.com/api/heroStats").responseJSON { response in

        if let value = response.result.value {

           let json = JSON(value)

        //Printing strings from a JSON Dictionary
        print(json[0]["localized_name"].stringValue)
        print(json[0]["primary_attr"].stringValue)
        print(json[0]["attack_type"].stringValue)
        print(json[0]["legs"].stringValue)
        print(json[0]["img"].stringValue)
       }
    } // End Alamofire call
  }
}

The request is async . 该请求是async When it gives you a result, you need to parse it and assign it to your array of HeroStat . 当它给您结果时,您需要对其进行解析并将其分配给HeroStat数组。 That needs to be done in your request completion handler. 这需要在您的请求完成处理程序中完成。 You are currently doing nothing after you get your response. 得到回应后,您目前无所事事 Also, your call to reloadData is done before you actually get a response, so that won't do. 另外,对reloadData的调用是在您实际获得响应之前完成的,因此不会进行。

What you could do is: Once you get your response, populate the heros array and call tableView.reloadData() to make it fetch the new populated HeroStat values. 您可以做的是:获得响应后,填充heros数组并调用tableView.reloadData()以使其获取新的填充的HeroStat值。

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

相关问题 在Alamofire 4中使用SwiftyJson为MultipartFormData请求创建JSON数据 - Creating JSON data using SwiftyJson for MultipartFormData request in Alamofire 4 如何使用Alamofire / SwiftyJSON将JSON数据加载到单元格中? - How to load JSON data into cells using Alamofire/SwiftyJSON? 如何使用Alamofire和SwiftyJSON从Json保存数据? - How to save data from Json with Alamofire and SwiftyJSON? 如何使用 SwiftyJSON 和 Alamofire 解析 JSON 字典 - How to parse JSON dictionary using SwiftyJSON and Alamofire 使用SwiftyJson的JSON解析器未使用Alamofire解析任何信息 - JSON parser using SwiftyJson is not parsing any information using Alamofire JSON 方法 POST 与 Alamofire 和 SwiftyJSON - JSON method POST with Alamofire and SwiftyJSON 使用Alamofire和SwiftyJSON正确解析带有多个对象的JSON数组 - Correctly parsing through a JSON array with multiple objects using Alamofire and SwiftyJSON 如何使用Alamofire SwiftyJSON将图像上传到JSON Swift 3 - How to upload an image using Alamofire SwiftyJSON to a JSON swift 3 Swift:使用 Alamofire 和 SwiftyJSON 处理 JSON - Swift: Handling JSON with Alamofire & SwiftyJSON 使用 alamofire 和 swiftyJson 从 UICollectionViewCell 内的 UICollectionView api 获取数据 - grab data from api for UICollectionView inside UICollectionViewCell using alamofire and swiftyJson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM