Loading data in NSUserDefaults

i have done the coding to save the highscore in NSuserdefaults, but i am not sure how can i load the data from the nsuserdefaults and display it in a table. PLease help.

NSString *name;

name = nametextbox.text;

NSDictionary *player = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@", name], @"name",[NSString stringWithFormat:@"%d", myScore], @"score",nil];
[highScore addObject:player];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"score" ascending:NO];
[highScore sortUsingDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

[[NSUserDefaults standardUserDefaults] setObject:highScore forKey:@"highScore"];


You should be able to load it just the way you'd expect (like accessing a value in an NSDictionary):

NSArray *highScore = [[NSUserDefaults standardUserDefaults] objectForKey:@"highScore"];


To display the data from this array in a table view, you'll need to create a view controller and use the array as your data source. The easiest way to do this is by subclassing UITableViewController. This should get you started on the implementation of that controller:

// HighScoreViewController.h

@interface HighScoreViewController : UITableViewController {
  NSArray *highScores;
@property (nonatomic, retain) NSArray *highScores;


// HighScoreViewController.m

#import HighScoreViewController.h

static const NSInteger kNameLabelTag = 1337;
static const NSInteger kScoreLabelTag = 5555;

@implementation HighScoreViewController
@synthesize highScores;

- (void)viewDidLoad {
  [self setHighScores:[[NSUserDefaults standardUserDefaults] 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
  return [self.highScores count];

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIdentifier = @"PlayerCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cel == nil) {
    cell = [[[UITableViewCell alloc]
             reuseIdentifier:cellIdentifier] autorelease];

    // Create UILabels for name and score and add them to your cell
    UILabel *nameLabel = [[UILabel new] autorelease];
    [nameLabel setTag:kNameLabelTag];
    [cell.contentView addSubview:nameLabel];

    UILabel *scoreLabel = [[UILabel new] autorelease];
    [scoreLabel setTag:kScoreLabelTag];
    [cell.contentView addSubview:scoreLabel];

    // Set other attributes common to all of your cells here
    // You will also need to set the frames of these labels (nameLabel.frame = CGRectMake(...))

  NSDictionary *player = [self.highScores objectAtIndex:indexPath.row];
  NSString *name = [player objectForKey:@"name"];
  NSString *score = [player objectForKey:@"score"];

  [(UILabel *)[cell.contentView viewWithTag:kNameLabelTag] setText:name];
  [(UILabel *)[cell.contentView viewWithTag:kScoreLabelTag] setText:score];

  return cell;


A key thing to remember with UITableView is that cells get reused, so you need to be careful about where you initialize/configure your cell's subviews.

Need Your Help

In WPF, how do I bind a textbox style to two different styles that toggle on a button click?

wpf triggers binding styles

I'm working in a MVVM paradigm, and I'm trying to get a textbox to change styles when a button is triggered.

Does anyone know when the ASP.NET MVC will be fully released?

.net asp.net asp.net-mvc

When do you think we can expect the full release version of ASP.NET MVC?