2014-01-12 65 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; 
} 
}