swift tableview displays json data very slow

て烟熏妆下的殇ゞ 提交于 2020-01-15 15:28:35

问题


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


来源:https://stackoverflow.com/questions/30625275/swift-tableview-displays-json-data-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!