本文介绍了将Game Center好友及其分数加载到UITableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在阅读完苹果文档后就想知道( 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:方法单独获取朋友列表和朋友分数.

So I was wondering after reading the apple docs(https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboard_Ref/Reference/Reference.html#//apple_ref/occ/instp/GKLeaderboard/category) how would one create a UITableView and fill it with the localPlayers Game Center friends and there scores in a specific leaderboard. I know how to get the friends list and friends scores individually by using the loadScoresWithCompletionHandler: method.

到目前为止,我已经做到了这一点,可以将单个朋友的照片,分数和显示名称保存到一个NSArray中.但是我不知道如何在UITableView中显示它们.

So far I got this to get individual friends photo, score and displayname saved into one NSArray. But i can't figure out how to disply them in a 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;
    }
}
}

谢谢大家,乔治

推荐答案

看看这个雷·温德利希(Ray Wenderlich)的教程,它说明了如何在UITableView中显示简单的图片和文本-分为三个部分,应该可以使您工作至少具有基本但可行的观点.

Take a look at this tutorial by Ray Wenderlich, it explains how to display simple pictures and text in a UITableView - there are three parts and should get you working with, at least, a basic but working view.

从根本上讲,这是完成在UITableView中显示的工作"的代码

At its very core level this is the code that does "the work" for displaying in a 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;
}

更新

这是我的代码,用于使用别名和照片生成排行榜数据,希望您可以对其进行适当的修改,但不要太过不同

Here is my code for generating leaderbord data with alias and photos, hope you can modify it appropriately but shouldnt be too different

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

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

这篇关于将Game Center好友及其分数加载到UITableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:16