在NSUserDefaults中加载数据 [英] Loading data in NSUserDefaults

查看:53
本文介绍了在NSUserDefaults中加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了将高分保存到NSuserdefaults中的编码,但是我不确定如何从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"];


推荐答案

您应该能够按照自己的方式加载它期望(例如访问 NSDictionary 中的值):

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

更新

要在表格视图中显示该数组中的数据,您需要创建一个视图控制器并将该数组用作数据源。最简单的方法是将 UITableViewController 子类化。这应该使您开始执行该控制器:

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

要记住的关键 UITableView 是单元格被重用,因此您需要注意在哪里初始化/配置单元格的子视图。

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.

这篇关于在NSUserDefaults中加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆