【问题标题】:Populate UITableView with Facebook friends用 Facebook 好友填充 UITableView
【发布时间】:2014-01-12 09:28:02
【问题描述】:

我正在尝试使用 Facebook 好友列表填充 UITabelView。我已经检查了所有相关问题并尝试了所有答案,但是仍然无法正常工作。 这个简单的代码浪费了我的时间已经4个多小时了。

@interface InviteViewController ()

{

__block NSArray *friends;
__block NSMutableArray *mArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends",friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
        [mArray addObject:friend.name];
    }

}];
NSLog(@"%d",mArray.count);
return mArray.count;
}

这是我的控制台日志:

2014-01-12 20:18:21.429 PixidizeStoryBoard[26269:60b] 0
2014-01-12 20:18:21.742 PixidizeStoryBoard[26269:60b] Found: 1 friends
2014-01-12 20:18:21.744 PixidizeStoryBoard[26269:60b] I have a friend named Siavash ALp with id 1524650444

【问题讨论】:

    标签: ios iphone xcode facebook uitableview


    【解决方案1】:

    看起来它正在工作。它正在寻找一位朋友。

    我认为对startWithCompletionHandler: 的调用是异步调用。所以它会调用标签,然后立即运行NSLog(@"%d",mArray.count); 并返回一个仍然为空的NSMutableArray。然后在后台线程调用这个块之后。

    ^(FBRequestConnection *connection,
                                                  NSDictionary* result,
                                                  NSError *error) {
        friends = [result objectForKey:@"data"];
        NSLog(@"Found: %i friends",friends.count);
        for (NSDictionary<FBGraphUser>* friend in friends) {
            NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
            [mArray addObject:friend.name];
        }
    

    你可以看到一个朋友正在登录到 NSLog,但这就是为什么它发生在你第一次调用 NSLog 之后。因此,与其尝试返回 NSMutableArray,不如在块的末尾添加一些内容来更新 UITableView。

    这有意义吗?

    【讨论】:

    • 谢谢。我以另一种方式解决了,我设法在加载(segue)UITableView之前获取朋友列表,然后将prepareForSegue方法中的朋友数组传递给UITableView,它可以完美运行。
    【解决方案2】:
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    //[self.spinnerView setHidden:YES];
    self.navigationController.title = @"Create Event";
    FBRequest* friendsRequest = [FBRequest requestForMyFriends];
    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                  NSDictionary* result,
                                                  NSError *error) {
        __block NSArray *friends;
    
        friends = [result objectForKey:@"data"];
        NSLog(@"Found: %i friends",friends.count);
        for (NSDictionary<FBGraphUser>* friend in friends) {
            NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
    
        }
        [self setArrayWithArray:friends];
    }];
    
    // Do any additional setup after loading the view.
    }
    - (void)setArrayWithArray:(NSArray *)array
    {
    self.myFriends = [NSMutableArray array];
    for (NSDictionary<FBGraphUser>* friend in array) {
        [self.myFriends addObject:friend.name];
    
    }
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:@"invite"]) {
        InviteViewController *ivc = segue.destinationViewController;
        ivc.myImage = self.img;
        ivc.nameField2 = self.nameField.text;
        ivc.locationField2 = self.locationField.text;
        ivc.friendsArray = self.myFriends;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多