抱歉,标题有点笼统,如果有人有更好的建议,请告诉我。
基本上我正在编写一个自定义排行榜视图,我只想显示 3 个分数。如果可能,它将在中间显示当前用户的分数,但如果用户位于列表的顶部或底部,它仍应显示 3 个分数,但它会在列表上方或下方显示其他用户。
例如
- 我(如果我名列前茅,请在下面显示 2)
- 用户 1
- 用户 2
或者
- 用户 1
- 我(通常情况下我处于两个分数的中间)
- 用户 2
或者
- 用户 1
- 用户 2
- 我(如果我垫底,显示比我高两个分数)
我编写了一个函数来完成第一部分,但没有考虑到我正在努力解决的边缘情况。有人可以请教吗?
-(void)getNearbyScores:(int)score{
GCLeaderboardScore *closestScoreAbove = nil; //Custom container for GC properties
GCLeaderboardScore *closestScoreBelow = nil; //Contains playerID, score, alias etc
if ([playerScores count] == 0){ //playerScores is an NSMutableDictionary
return;
}
for (NSString* key in playerScores) {
GCLeaderboardScore *playerScore = (GCLeaderboardScore *)[playerScores objectForKey:key];
if ((closestScoreAbove == nil || closestScoreAbove->score > playerScore->score) && playerScore->score > score){
closestScoreAbove = playerScore;
}
else if ((closestScoreBelow == nil || closestScoreAbove->score < playerScore->score) && playerScore->score < score){
closestScoreBelow = playerScore;
}
}
me->score = score;
me->rank = 1;
if (closestScoreAbove != nil) {
me->rank = closestScoreAbove->rank + 1;
nearbyScores = [NSMutableArray arrayWithObjects: closestScoreAbove, me, closestScoreBelow, nil];
}
else {
nearbyScores = [NSMutableArray arrayWithObjects: me, closestScoreBelow, nil];
}
}