迅速的tableview显示JSON数据非常慢 [英] swift tableview displays json data very slow

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

问题描述

我有一个循环JSON数据并在tableview中显示的页面. 我正确地获得了正确的信息,但是当它显示在页面上时,它的运行速度非常慢. 我尝试对json进行打印,以查看它检索json数据的速度,而且速度非常快. 还有一件很奇怪的事情是加载时,如果我拖动页面,所有内容都会立即显示出来. 下面是我放入viewdidload函数中的代码

I have a page that loops JSON data and show in a tableview. I get the correct correctly but when it shows on the page, it is very slow. I tried to println the json to see how fast it retrieves the json data and it was very fast. One more thing which is very odd is when it is loading, if I drag the page, everything will show instantly. Below is the code I put in viewdidload function

        self.PoTable.separatorStyle = UITableViewCellSeparatorStyle(rawValue: 0)!
        self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
        PoTable.delegate = self
        PoTable.dataSource = self
        self.PoTable.addSubview(self.activityIndicatorView)
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        activityIndicatorView.startAnimating()
        let url = NSURL(string: SharedClass().clsLink + "/json/POList.cfm")
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
            if(error != nil) {
                // If there is an error in the web request, print it to the console
                println(error.localizedDescription)
            }
            var err: NSError?
            let res = response as! NSHTTPURLResponse!
            if(res != nil){
                if (res.statusCode >= 200 && res.statusCode < 300){
                    self.jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
                    if(err != nil) {
                        // If there is an error parsing JSON, print it to the console
                        println("JSON Error \(err!.localizedDescription)")
                    }
                    var resultsArr: NSArray = self.jsonResult["results"] as! NSArray
//                        println(resultsArr)
                    self.PoList = PoInfo.poInfoWithJSON(resultsArr)
                    self.PoTable!.reloadData()
                    self.activityIndicatorView.stopAnimating()
                    self.activityIndicatorView.hidden = true
                }
                else{
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    SharedClass().serverAlert(self)
                }
            }else{
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                self.activityIndicatorView.stopAnimating()
                SharedClass().serverAlert(self)
            }
        })
        task.resume()

请帮助

推荐答案

在if闭包之类的

  dispatch_async(dispatch_get_main_queue()) {
             if (res.statusCode >= 200 && res.statusCode < 300){
                self.jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
                if(err != nil) {
                    // If there is an error parsing JSON, print it to the console
                    println("JSON Error \(err!.localizedDescription)")
                }
                var resultsArr: NSArray = self.jsonResult["results"] as! NSArray
                self.PoList = PoInfo.poInfoWithJSON(resultsArr)
                self.PoTable!.reloadData()
                self.activityIndicatorView.stopAnimating()
                self.activityIndicatorView.hidden = true
            }
  }

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

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