开发者

how do i display the data saved in table

I have save my data into NSUserDefaults but i do not know how to display it into the UITable.

-(IBAction)savehighscore_button {

    int i, ii = -1;

    struct high_score {
        NSString *name;
        int highScore;
    }; 

    NSString *nameentered;

    nameentered = nametextbox.text;

    struct high_score structArray[10];

    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];

    for (i=0; i<10; i++) {

        if ([userPreferences stringForKey :[NSString stringWithFormat:@"highScoreNameEntry%d",i]] !=nil && [userPreferences stringForKey :[NSString stringWithFormat:@"highScoreEntry%d"]] !=nil) {
            structArray[i].name= [userPreferences stringForKey:[开发者_Python百科NSString stringWithFormat:@"highScoreNameEntry%d",i]];
            structArray[i].highScore = [userPreferences integerForKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];
            ii = i;
        }
    }
    if (myScore >0) {
        for (i==ii; i>=0; i--) {
            if (myScore > structArray[i].highScore) {
                if (i<9) 
                    structArray[i+1] = structArray[i];
                    structArray[i].name = nameentered;
                    structArray[i].highScore = myScore;

                    if (i==ii && i<9) 

                        ii=i+i;
                    }

                    else if(i==ii && i<9) {
                        structArray[i+1].name = nameentered;
                        structArray[i+1].highScore = myScore;
                        ii=i+1;
                    }
                }
            }

            if (ii==-1 && myScore >0) {
                structArray[0].name = nameentered;
                structArray[0].highScore = myScore;
                ii=0;
            }
            for (i=0; i<=ii; i++) {
                [userPreferences setObject:structArray[i].name forKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]];
                [userPreferences setInteger:structArray[i].highScore forKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];

            }

    [userPreferences synchronize];

    [nametextbox resignFirstResponder];

    [self viewDidLoad];
}


In order you use a UITableView you need to implement UITableViewDelegate and UITableViewDatasource methods. (Or better said, your UITableView needs a datasource and delegate. For instance lets say your view controller implements them)

Now, instead of having a different key for each row you probably want to save all rows in an array (NSArray/NSMutableArray) and save the hole array using NSUserDefaults. Since NSArray only accepts NSObjects and no c structs, to put your scores in a NSArray/NSMutableArray you need another class defined by yourself or you can use an NSDictionary. This will be your Model. (Remember MVC? ;) )

Once you have this then its quite easy to show the score in a UITableView.

This is one way of converting your structArray into an NSMutableArray

#define kScoreName @"scoreName"
#define kHighScore = @"highScore"
#define kDatasource = @"datasource"


NSMutableArray *datasourceArray = [[NSMutableArray array] retain]; //this should be a instance variable
for (i=0; i<10; i++) {
    NSDictionary *score = [NSDictionary dictionaryWithObjectsAndKeys:structArray[i].name, kScoreName, [NSNumber numberWithInt:structArray[i].highScore], kHighScore, nil];
    [datasourceArray addObject:score];
}

NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:datasourceArray forKey:kDatasource];

Now you only need to do [datasourceArray objectAtIndex:i] to get the i-th element.

Having this array you just need to follow any of tutorial on UITableView, this is the first in my google search. ;) Or you can see many Apple samples

This is an example of how you would implement one of the methods:

- (UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
        reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];
    NSDictionary *score = [datasourceArray objectAtIndex:row];

    cell.textLabel.text = [score objectForKey:kScoreName];
    cell.detailTextLabel.text = [[score objectForKey:kHighScore] intValue];
    return cell;

}

@end

Hope it helps ;)

Note: above code is brain compiled ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜