简体   繁体   English

直到在iOS中选中,UITableView中的单元格才会更新(快速)

[英]Cell in UITableView doesn't update until selected in iOS (swift)

I'm developing an app with Parse that has a tableview with cells containing a label that has mutual friends from Facebook, my problem is that everything thing in the table works fine but the mutual friends label, it takes long to show unless I select the row (when I select the row the number appears immediately).. Here's my code for getting the mutual friends and setting the label in the cell: 我正在使用Parse开发一个应用程序,该应用程序具有带有包含来自Facebook共同朋友的标签的单元格的表格视图,我的问题是表中的所有内容都可以正常运行,但是共同朋友标签需要花很长时间才能显示,除非我选择行(当我选择行时,数字会立即显示)。这是获取共同朋友并在单元格中设置标签的代码:

let facebookContext = driverobj?.objectForKey("facebookContext") as! String
let user: PFUser = PFUser.currentUser()!
let access_token = user.valueForKey("authData")?.valueForKey("facebook")?.valueForKey("access_token") as! String
let usercontexturl: NSURL = NSURL(string: "https://graph.facebook.com/\(facebookContext)/mutual_friends?access_token=\(access_token)")!

let myrequest: NSURLRequest = NSURLRequest(URL: usercontexturl)
let mysession = NSURLSession.sharedSession()
let task = mysession.dataTaskWithRequest(myrequest) { data, response, error in
        print("Task completed")
        do {
             let jsonresult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
             cell.mutualFriendsLabel?.text = (jsonresult.valueForKey("summary")?.valueForKey("total_count"))!.stringValue + " Mutual Friends"

        } catch {
             print(error)
        }
}
task.resume()

This code is inside cellForRowAtIndexPath method, and didSelectRowAtIndexPath method in empty. 此代码位于cellForRowAtIndexPath方法内部,并且didSelectRowAtIndexPath方法为空。

The block passed to dataTaskWithRequest is probably not executing on the main thread which can cause these types of symptoms to appear. 传递给dataTaskWithRequest的块可能不在主线程上执行,这可能导致这些类型的症状出现。 Try executing the UI updates to the main thread like: 尝试对主线程执行UI更新,例如:

dispatch_async(dispatch_get_main_queue()) {
    cell.mutualFriendsLabel?.text = (jsonresult.valueForKey("summary")?.valueForKey("total_count"))!.stringValue + " Mutual Friends"
}

I think you need to update UI on main queue like this: 我认为您需要像这样在主队列上更新UI:

dispatch_async_(your_queue) {
    task.response {
        //handle data
        dispatch_async(dispatch_get_main_queue()) {
            //update UI, etc. change label text
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM