lol
lol

Reputation: 23

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"];

Upvotes: 0

Views: 898

Answers (1)

Cameron Spickert
Cameron Spickert

Reputation: 5200

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"];

Update

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;
@end

.

// 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] 
                       objectForKey:@"highScore"]];
}

- (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]
             initWithStyle:UITableViewCellStyleDefault
             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;
}

@end

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.

Upvotes: 1

Related Questions