【问题标题】:load Game Center friends and their scores into UITableView将 Game Center 好友及其分数加载到 UITableView
【发布时间】:2013-10-11 17:05:45
【问题描述】:

所以我在阅读苹果文档 (https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboard_Ref/Reference/Reference.html#//apple_ref/occ/instp/GKLeaderboard/category) 后想知道如何创建一个 UITableView 并用 localPlayers 游戏中心的朋友填充它,并在特定的排行榜中获得分数。我知道如何使用 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;
    }
}
}

谢谢各位, 乔治

【问题讨论】:

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


    【解决方案1】:

    看看这个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;
    }
    

    更新

    这是我用别名和照片生成排行榜数据的代码,希望你可以适当地修改它,但不应该有太大的不同

    -(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);
                             }
    
                         }];
                     }
                 }];
            }
        }];
    }
    

    【讨论】:

    • 感谢您的帮助!但现在我遇到了一些问题。我认为检索 localPlayers 好友分数、显示名称和照片的代码不正确。你怎么看?
    • 首先我不认为它为每个玩家创建一个 NSArray 并有各自的照片、显示名称和分数我认为它创建 1 很麻烦。你可以发布你的代码吗?跨度>
    • 用我用来生成排行榜的代码更新了我的答案,我稍后将它直接放入 UITableView(排序后),所以应该可以很好地工作。 playerScoresNSMutableDictionaryGCLeaderboardScore 基本上是一个客观的 c 结构来存储数据,它没有什么特别的
    • 那么你如何从 NSMutable 数组中检索数据?对不起我的菜鸟。
    • playerScore 是我自己的自定义类GCLeaderboardScore 不是GKLeaderBoardSet。它只是一个具有这些属性的基本类。
    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多