ios - tableView 没有快速显示来自 JSON 的数据

标签 ios json swift

我正在解析来自 HERE 的 JSON 数据.

为此我使用了这段代码。

DataManager.swift

import Foundation

let TopAppURL = "http://api.feedzilla.com/v1/categories.json"

class DataManager {

class func getTopAppsDataFromItunesWithSuccess(success: ((iTunesData: NSData!) -> Void)) {
    //1
    loadDataFromURL(NSURL(string: TopAppURL)!, completion:{(data, error) -> Void in
        //2
        if let urlData = data {
            //3
            success(iTunesData: urlData)
        }
    })
}

class func loadDataFromURL(url: NSURL, completion:(data: NSData?, error: NSError?) -> Void) {
var session = NSURLSession.sharedSession()

// Use NSURLSession to get data from an NSURL
let loadDataTask = session.dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
  if let responseError = error {
    completion(data: nil, error: responseError)
  } else if let httpResponse = response as? NSHTTPURLResponse {
    if httpResponse.statusCode != 200 {
      var statusError = NSError(domain:"com.raywenderlich", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
      completion(data: nil, error: statusError)
    } else {
      completion(data: data, error: nil)
    }
  }
})

loadDataTask.resume()
 }
}

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var i = 0
var detailid = [Int]()
var detailCat = [String]()
var tableData = ["1","2","3"]

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()

DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
    
    let json = JSON(data: iTunesData)
    
    if let array = json.arrayValue{

        for Dict in array{
            var id : Int = array[self.i]["category_id"].integerValue!
            var category : String = array[self.i]["english_category_name"].stringValue!
            self.detailid.append(id)
            self.detailCat.append(category)
            
            self.i++
            
        }
        println(self.detailid)
        println(self.detailCat)
    }
}


}

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


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    cell.textLabel.text = self.detailCat[indexPath.row]
    return cell
    
   }

}

在控制台中,我得到的数据如下:

[1314, 13, 21, 22, 5, 588, 6, 17, 25, 1168, 11, 14, 2, 28, 15, 33, 591, 20, 29, 36, 3, 10, 16, 18, 8, 34, 4, 27, 30, 31, 26, 23, 12, 7, 590, 9, 19]
[Sports, Art, Blogs, Business, Celebrities, Columnists, Entertainment, Events, Fun Stuff, General, Health, Hobbies, Industry, Internet, IT, Jobs, Law, Life Style, Music, Oddly Enough, Politics, Products, Programming, Religion And Spirituality, Science, Shopping, Society, Sports, Technology, Top Blogs, Top News, Travel, Universities, USA, Video, Video Games, World News]

但它没有将其打印到 tableView 中。

我错过了什么?请帮助我。

最佳答案

viewDidLoad 中的 for 循环 完成并获得数据后,您必须调用:

tableView.reloadData()

关于ios - tableView 没有快速显示来自 JSON 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719334/

相关文章:

android - 移动图书馆或图片库示例

javascript - amcharts - 图表不显示值/使用 ajax 绘制 amcharrt/如何通过查询数据库绘制 amchart

swift - 如何在 [Int] Swift 中存储颜色代码

python - KeyError at/object_post//由于python或JSON而发生?

ios - 如何在水平堆栈 View 中居中按钮?

ios - 在 Swift 中使用自定义模型将 CoreData 模型添加到现有项目的最佳方法

ios - 将添加的行设置为第一个 indexPath 行

ios - UIImageView 设置为 imageWithData 忽略 contentMode

ios - PhoneGap-iOS,如何显示带有红色按钮的对话框?

javascript - Angular 2 : trying to get a project in the detail page