tableView没有快速显示来自JSON的数据 [英] tableView is not showing data from JSON in swift

查看:70
本文介绍了tableView没有快速显示来自JSON的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 HERE 解析JSON数据.

I am parsing JSON data from HERE.

为此,我使用了这段代码.

for that I have used this code.

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
    
   }

}

在控制台中,我正在获取如下数据:

In console I am getting data like:

[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中.

but It is not printing it into tableView.

我想念什么?请为此帮我.

what I am missing? please help me for this.

推荐答案

viewDidLoad中的 for循环完成后,您有了数据,就必须调用:

After your for loop in viewDidLoad is finished, and you got your data, you have to call :

tableView.reloadData()

这篇关于tableView没有快速显示来自JSON的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆