我正在开发小游戏。我正在使用 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];
}