Ios 使用Facebook好友填充UITableView

Ios 使用Facebook好友填充UITableView,ios,iphone,xcode,facebook,uitableview,Ios,Iphone,Xcode,Facebook,Uitableview,我正在尝试用Facebook好友列表填充UITableView。我已经检查了所有相关的问题,并尝试了所有的答案,但是,它仍然不起作用。 这段简单的代码浪费了我4个多小时的时间 @interface InviteViewController () { __block NSArray *friends; __block NSMutableArray *mArray; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInS

我正在尝试用Facebook好友列表填充UITableView。我已经检查了所有相关的问题,并尝试了所有的答案,但是,它仍然不起作用。 这段简单的代码浪费了我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;
}

看起来它在工作。找到一个朋友

我认为对
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];
    }
^(FBRequestConnection*连接,
NSDictionary*结果,
N错误*错误){
friends=[result objectForKey:@“数据”];
NSLog(@“找到:%i个朋友”,朋友数);
用于(NSDictionary*朋友中的朋友){
NSLog(@“我有一个名为%@且id为%@”、friend.name、friend.id的朋友);
[mArray addObject:friend.name];
}
您可以看到一个朋友正在登录到NSLog,但这就是为什么在您第一次调用NSLog后会发生这种情况。因此,与其尝试返回NSMutableArray,不如在块的末尾添加一些更新UITableView的内容

这有意义吗?

-(void)viewDidLoad
- (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;
}
}
{ [超级视图下载]; //[self.spinnerView setHidden:是]; self.navigationController.title=@“创建事件”; FBRequest*friendsRequest=[FBRequestFormyFriends]; [FriendsRequestStartWithCompletionHandler:^(FBRequestConnection*连接, NSDictionary*结果, N错误*错误){ __阻止NSArray*朋友; friends=[result objectForKey:@“数据”]; NSLog(@“找到:%i个朋友”,朋友数); 用于(NSDictionary*朋友中的朋友){ NSLog(@“我有一个名为%@且id为%@”、friend.name、friend.id的朋友); } [self-setArrayWithArray:friends]; }]; //加载视图后执行任何其他设置。 } -(void)setArrayWithArray:(NSArray*)数组 { self.myFriends=[NSMutableArray]; for(NSDictionary*数组中的朋友){ [self.myFriends addObject:friend.name]; } } -(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方 { if([segue.identifier IsequalString:@“invite”]){ InviteViewController*ivc=segue.destinationViewController; ivc.myImage=self.img; ivc.nameField2=self.nameField.text; ivc.locationField2=self.locationField.text; ivc.friendsArray=self.myFriends; } }
谢谢。我用另一种方法解决了这个问题,我在加载(segue)UITableView之前获得了好友列表,然后将prepareForSegue方法中的好友数组传递给UITableView,它工作得非常好。
- (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;
}
}