Swifty JSON UITableView背景错误

时间:2015-09-24 07:49:05

标签: ios json uitableview swifty-json

我已经按照Ray Wenderlich的教程进行了操作。但是我没有将数组直接导入UITableView。现在大约需要20秒才能将JSON加载到UITableView中。但在控制台日志中,阵列是直接可用的。我在Swift 2.0工作。

@IBOutlet var tableView: UITableView!

let textCellIdentifier = "CoffeeLocations"
var apps : [String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    // Do any additional setup after loading the view.
    DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
        let json = JSON(data: iTunesData)

        if let appArray = json[].array {

            for appDict in appArray {
                let appName: String? = appDict["name"].string
                self.apps += ["\(appName!)"]
            }

            print(self.apps) // Here he print directly to the console log
            self.tableView.reloadData()
        }
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! TableViewCell
    let row = indexPath.row
    cell.titleLabel!.text = apps[row]
    //cell.detailLabel!.text = description[row] Later I want to add a second rule
    return cell
}


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

错误日志(直接):

  

2015-09-24 09:45:31.493 TopApps [660:180190] 收到内存警告。    [“\''Koffiehuisje”,“Caffee Allee | Stadsrestaurant”,“Ketelhuis”,“Monk Eindhoven”,“Radio Royaal”,“PopEi Food& Drinks”,“Tijdelijk restaurant”,“NATLAB”,“ Bagel& Juice“,”Pastry club“,”Intellegentia ICE“,”Onder de leidingstraat“,”Keukenconfessies“]

数组中每一行的错误日志(20秒后):

  

2015-09-24 09:46:09.579 TopApps [660:180231] 此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃。这将在将来的版本中引发异常。    堆:(       0 CoreFoundation 0x0000000182bb0f74 + 148       1 libobjc.A.dylib 0x00000001977a3f80 objc_exception_throw + 56       2 CoreFoundation 0x0000000182bb0ea4 + 0       3基金会0x0000000183bca5d8 + 88       4基金会0x0000000183a4ca1c + 36       5 UIKit 0x000000018820b958 + 64       6 UIKit 0x00000001881022d8 + 240       7 UIKit 0x0000000188101b8c + 116       8 UIKit 0x0000000188101a18 + 504       9 UIKit 0x00000001884098e0 + 228       10 UIKit 0x0000000188100458 + 412       11 UIKit 0x00000001881fae14 + 1440       12 UIKit 0x00000001881efab8 + 216       13 UIKit 0x000000018810300c + 644       14 QuartzCore 0x0000000187909f14 + 148       15 QuartzCore 0x0000000187904b20 + 292       16 QuartzCore 0x00000001879049e0 + 32       17 QuartzCore 0x000000018790407c + 252       18 QuartzCore 0x0000000187903dd0 + 516       19 QuartzCore 0x0000000187932f48 + 236       20 libsystem_pthread.dylib 0x00000001981b21e8 + 584       21 libsystem_pthread.dylib 0x00000001981b1d60 + 136       22 libsystem_pthread.dylib 0x00000001981b1544 pthread_mutex_lock + 0       23 libsystem_pthread.dylib 0x00000001981b1028 start_wqthread + 4   )

1 个答案:

答案 0 :(得分:1)

dispatch_async(dispatch_get_main_queue(), {
    // put your code here  load the JSON into the UITableView
})