【问题标题】:Cell in UITableView doesn't update until selected in iOS (swift)UITableView 中的单元格在 iOS 中选择之前不会更新(swift)
【发布时间】:2016-05-17 10:17:21
【问题描述】:

我正在使用 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()

此代码在 cellForRowAtIndexPath 方法中,didSelectRowAtIndexPath 方法为空。

【问题讨论】:

    标签: ios swift uitableview facebook-graph-api parse-platform


    【解决方案1】:

    传递给dataTaskWithRequest 的块可能没有在主线程上执行,这可能导致出现这些类型的症状。尝试对主线程执行 UI 更新,例如:

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

    【讨论】:

      【解决方案2】:

      我认为您需要像这样更新主队列上的 UI:

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

      【讨论】:

        猜你喜欢
        • 2015-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多