将游戏中心的朋友和他们的分数加载到UITableView中

时间:2013-10-11 17:05:46

标签: ios objective-c uitableview game-center gamekit

所以我在阅读苹果文档(https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboard_Ref/Reference/Reference.html#//apple_ref/occ/instp/GKLeaderboard/category)之后想知道如何创建一个UITableView并将其填充到localPlayers Game Center的朋友那里并在特定的排行榜中得分。我知道如何使用loadScoresWithCompletionHandler:方法单独获取好友列表和好友分数。

编辑:到目前为止,我得到了这个,以便将个人朋友的照片,得分和显示名称保存到一个NSArray中。但我无法弄清楚如何在UITableView中显示它们。

- (void) loadPlayerData: (NSArray *) identifiers
{
    GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
    if (leaderboardRequest != nil) {
        leaderboardRequest.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
        leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
        leaderboardRequest.category = @"MJ_IL";
        [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
            if (error != nil) {
                    // handle the error. if (scores != nil)
           }

        if (scores != nil){
        for (GKScore* score in scores) {

            NSArray *playerIdArray = [NSArray arrayWithObject:score.playerID];
            [GKPlayer loadPlayersForIdentifiers:playerIdArray withCompletionHandler:^(NSArray *players, NSError *error) {

                GKPlayer *player = [players objectAtIndex:0];
                [player loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error) {

                    if (score.playerID == player.playerID) {
                        if (photo != nil) {
                            playerInfo = [NSArray arrayWithObjects:score, player.displayName, photo, nil];

                        } else if (photo == nil) {

                             playerInfo = [NSArray arrayWithObjects:score, player.displayName,  nil];

                        }
                        if (error != nil) {
                            NSLog(@"%@", error.localizedDescription);
                        }
                    }

                }];
            }];
        }
    }
    }];
}
}

- (void)compareLocalPlayerScoreWithFriends {

GKScore *friendScore = [playerInfo objectAtIndex:0];
NSString *friendDisplayName = [playerInfo objectAtIndex:1];
if ([playerInfo objectAtIndex:2] != nil) {

    UIImage *friendPhoto = [playerInfo objectAtIndex:2];
    if (friendScore.value > interactiveHighscore) {

        [friendNameLabel setText:friendDisplayName];
        [friendScoreLabel setText:(NSString *)friendScore];
        friendImageView.image = friendPhoto;
    }
}
}

谢谢你们, 乔治

1 个答案:

答案 0 :(得分:5)

看一下这个tutorial by Ray Wenderlich,它解释了如何在UITableView中显示简单的图片和文字 - 有三个部分,应该让你至少可以使用基本的工作视图。

在其核心级别,这是用于在UITableView中显示“工作”的代码

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"MyBasicCell"];
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
    cell.textLabel.text = bug.data.title;
    cell.imageView.image = bug.thumbImage;
    return cell;
}

<强>更新

这是我用代码和照片生成leaderbord数据的代码,希望你能适当地修改它但不应该太差异

-(void)getScoresAndAliasForLeaderboard:(GKLeaderboard *)leaderboardRequest{
    if (leaderboardRequest == nil)
    {
        leaderboardRequest = [[GKLeaderboard alloc] init];
        leaderboardRequest.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
        leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
        leaderboardRequest.category = @"HighScore";
        leaderboardRequest.range = NSMakeRange(1,100);
    }

    [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
        if (error != nil)
        {
            // Handle the error.
        }
        if (scores != nil)
        {
            NSMutableArray *retrievePlayerIDs = [[NSMutableArray alloc] init];

            for (GKScore *s in scores)
            {
                [retrievePlayerIDs addObject:s.playerID];

                GCLeaderboardScore *playerScore = [[GCLeaderboardScore alloc] init];
                playerScore->playerID = s.playerID;
                playerScore->score = (int)s.value;
                playerScore->rank = s.rank;
                playerScores[s.playerID] = playerScore; //playerScores is a NSMutableDictionary

                if ([s.playerID isEqualToString: leaderboardRequest.localPlayerScore.playerID]){
                    me = playerScore;
                }
            }

            if (me == nil){
                me = [[GCLeaderboardScore alloc] init];
                me->playerID = leaderboardRequest.localPlayerScore.playerID;
                me->score = leaderboardRequest.localPlayerScore.value;
                me->alias = @"Me";

                playerScores[me->playerID] = me;
            }

            [GKPlayer loadPlayersForIdentifiers:retrievePlayerIDs withCompletionHandler:^(NSArray *playerArray, NSError *error)
             {
                 for (GKPlayer* p in playerArray)
                 {
                     GCLeaderboardScore *playerScore = playerScores[p.playerID];

                     playerScore->alias = p.alias;

                     [p loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error) {

                         if (photo != nil) {
                             playerScore->photo = photo;
                         }
                         else{
                             playerScore->photo = [UIImage imageNamed:@"wordpress_avatar.jpg"];
                         }
                         if (error != nil) {
                             NSLog(@"%@", error.localizedDescription);
                         }

                     }];
                 }
             }];
        }
    }];
}