gpt4 book ai didi

json - swift tableview显示json数据很慢

转载 作者:行者123 更新时间:2023-11-28 07:05:59 24 4
gpt4 key购买 nike

我有一个循环 JSON 数据并显示在表格 View 中的页面。我正确地得到了正确的,但是当它显示在页面上时,它非常慢。我尝试打印 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 闭包中尝试这个异步作用域 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
}
}

关于json - swift tableview显示json数据很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30625275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com