TableView 中的 Facebook 好友列表 : Think need Async Programming?

标签 facebook swift uitableview asynchronous

我正在尝试从 Facebook 获取好友列表,我在 tableview cellforrowatindexpath 中有调用请求,但我也在一个单独的函数中也有它。想尝试两种方法,但得到相同的结果。

如果我从完成 block 内部进行打印,我确实会得到结果,但是,如果我尝试在完成 block 外部打印friendProfile,它会返回nil,因此当我创建表格单元格时,它们会解开nil并且代码失败。我认为这是因为完成 block 直到我调用创建单元格后才返回数据。我正在阅读,也许这与异步编程有关?或者需要等待 Facebook 返回数据?或者完成处理程序总是返回 nil?

`

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: ["fields":"user_id,first_name,last_name,name,picture.type(large)"]);
    request.startWithCompletionHandler ({ (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
        if error == nil {
            //print("Friends are : \(result)")
            let jsonResult = result
            self.friendsArray = jsonResult["data"] as! NSArray
            var index = 0
                for data in self.friendsArray {
                    let fn = data["first_name"]! as! String
                    let ln = data["last_name"]! as! String
                    let id = data["id"]! as! String
                    let picture = data["picture"] as! NSDictionary
                    let parsePic = picture["data"] as! NSDictionary
                    let url = parsePic["url"] as! String
                    self.friendProfile .updateValue((fn + " " + ln), forKey: id)
                    self.friendProfilePic .updateValue(url, forKey: id)
                    self.friendProfileID . updateValue(id, forKey: index)
                    index++
                }

            } else {
            print("Error Getting Friends \(error)");
            }
    //if i do a print(friendprofilId) in here it works 
    })

   //but a print(friendprofilId) here returns nil
    let cellidentifier = "SettingsTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as! SettingsTableViewCell
    let id = self.friendProfileID[indexPath.row]
    print(self.friendProfileID)
    if (id != nil) {
        let name = self.friendProfile[id!]
        let url = self.friendProfilePic[id!]
       // cell.imageView?.image = returnFriendProfileImage(url!)
        cell.textLabel!.text = name
    }

    return cell
}

`

最佳答案

您需要在注释其有效的地方调用tableView.reloadData()。另外,请勿在 cellForRowAtIndexPath 中检索数据。只需调用 viewDidLoad

中的函数即可

关于TableView 中的 Facebook 好友列表 : Think need Async Programming?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34147683/

相关文章:

json - Swift 中的 JSON 文本不显示

ios 8 swift - 如何使用单独的数据源为 tableview 添加页脚

ios - insertSublayer 将隐藏表格 View 中的单元格

ios - 适用于iOS的Facebook SDK权限问题

javascript - channel 网址

ios - 如何使用 DispatchGroup 在 for 循环中进行异步调用

ios - 当我反向导航 UITableViewController 时应用程序崩溃,为什么?

facebook - Open Graph 如何 og :see_also tags intended to behave in Facebook?

php - Facebook/Twitter/Google+ 自定义对话框和带有计数器的按钮?

iphone - 更改 UITableView 部分标题的颜色