swift tableview displays json data very slow

2019-08-06 02:38发布

问题:

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()

please help

回答1:

try this async scope dispatch_async(dispatch_get_main_queue()) in your if closure like

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