0

我正在开发小游戏。我正在使用 nsuserdefaults 来存储玩家姓名和分数。现在我只想在我的 uitableview 中存储前 5 个分数。我只想显示前 5 个分数。

-(void)btnSaveScore

{
    if(!dictWinData)
        dictWinData = [[NSMutableDictionary alloc] init];

array = [[NSMutableArray alloc] init];
array = [[[NSUserDefaults standardUserDefaults] valueForKey:@"ScoreName"] mutableCopy];

if([array count] == 0)
{
    array = [[NSMutableArray alloc] init];
}

NSString *strName = [NSString stringWithFormat:@"%@",strNameOFPlayer];
NSString *strScore = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",iTap]];

if ([strScore length] == 3)
{

    [array addObject:[NSString stringWithFormat:@"%@                    %@",strScore,strName]];
}
else
{
    [array addObject:[NSString stringWithFormat:@"%@                      %@",strScore,strName]];
}

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
[sorter release];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sorters];

NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
[dfltsData setObject:sortedArray forKey:@"ScoreName"];
[dfltsData synchronize];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                message:@"Score is saved."
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
[alert show];    
[alert release];  

}
4

2 回答 2

4

做这个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([array count]>5)    
        return 5;
    else 
        return [array count];
}
于 2012-09-07T11:43:05.980 回答
0

使用简单的功能怎么样。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
    NSArray *scoresArray = [dfltsData objectForKey:@"ScoreName"];
    return MIN(5, [scoresArray count]);
}
于 2012-09-07T11:44:11.937 回答