Swift tableview显示json数据非常慢



我有一个页面,循环JSON数据,并在表视图中显示。我得到正确的正确,但当它显示在页面上,它是非常慢的。我试着打印json,看看它检索json数据的速度有多快,它非常快。还有一件很奇怪的事,当它加载时,如果我拖拽页面,一切都会立即显示出来。下面是我放入viewdidload函数

的代码
        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闭包中试试这个async作用域dispatch_async(dispatch_get_main_queue())

  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
            }
  }

相关内容

最新更新