2014-01-12 41 views
0

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

@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 

回答

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的东西。

这有道理吗?

+0

谢谢。我用另一种方式解决了,在加载(segue)UITableView之前,我设法获取好友列表,然后将prepareForSegue方法中的朋友数组传递给UITableView,并且它完美地工作。 – user2427421

0
- (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; 
} 
}