gpt4 book ai didi

ios - 在 ios swift 中延迟在 tableView 上显示数据

转载 作者:行者123 更新时间:2023-11-28 11:19:18 25 4
gpt4 key购买 nike

Hi,

I'm trying to parse data on to the listview and am able to get it and display it on the tableView but the problem is it is taking hell lot of time to display it on the tableview. Please find the my code below.

func jsonParsing()
{
activityIndicatorView.startAnimating()

let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest (URL: deviceListURL)
request.HTTPMethod = "GET"

let task = session.dataTaskWithRequest(request, 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?
if(data != nil) {
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSMutableArray

//println("Data: \(jsonResult)")

var dataDict: NSDictionary
for dataDict : AnyObject in jsonResult {
var device_id: NSString = dataDict.objectForKey("deviceId") as! NSString
var device_name: NSString = dataDict.objectForKey("deviceName") as! NSString
var device_status: NSInteger = dataDict.objectForKey("status") as! NSInteger

let dictionary = [self.deviceID: device_id, self.deviceName: device_name, self.Status: device_status]
self.myObject.addObject(dictionary)
}

println("My object = %@", self.myObject)
println(self.myObject.count)

if self.myObject.count != 0 {
self.reloadTable()

}
}

if err != nil {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}


})

task.resume()

}

最佳答案

完成处理程序在后台队列中运行,而不是主线程。不过,UI 更新必须在主线程上进行。

尝试在主线程上调用reloadTable():

dispatch_sync(dispatch_get_main_queue(), {
self.reloadTable()
})

(我只是在这里输入这个未经测试,所以我希望它能这样工作)

关于ios - 在 ios swift 中延迟在 tableView 上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666362/

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