2010-12-02 4 views
0

NSuserdefaults의 최고 점수를 저장하는 코드를 작성했지만 nsuserdefaults에서 데이터를로드하고 테이블에 표시하는 방법을 모르겠습니다. 도와주세요.NSUserDefaults의 데이터로드

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

@Jonathan Sterling이 최근에 작성한 의견 중 일부를 검토 한 결과, 한 단계 또는 두 단계를 줄이려고 할 수 있습니다. –

+0

동의하지 않습니다. (또한 누구든지 고칠 수 있습니다.) 감사합니다. 유효한 내용을 가지고 있지만 엄청난 철자법과 빈약 한 진술에 의해 손상된 많은 질문이 있습니다. 적어도 새로운 사용자로부터 기대할 수있는 최소한의 문제라고 생각합니다. 최소한 질문을 일관되게 취하고 적절한 용어와 닮은 것을 사용하십시오. –

+0

또한 Markdown 구문 가이드에 연결하면 건설적입니다. 그게 장난 치는 트롤과 좌절감을 가진 사람 사이의 차이점입니다. –

답변

1

당신은 (AN NSDictionary의 값에 접근처럼) 당신이 기대 다만 방법을로드 할 수 있어야한다 :의 데이터를 표시하려면

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

업데이트

을 이 배열을 테이블 뷰로 만들려면 뷰 컨트롤러를 만들고 배열을 데이터 소스로 사용해야합니다. 이를 수행하는 가장 쉬운 방법은 UITableViewController을 서브 클래 싱하는 것입니다. 이렇게하면 해당 컨트롤러의 구현을 시작할 수 있습니다.

// 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로 기억해야 할 중요한 점은 세포를 다시 얻을 것입니다, 그래서 당신은 당신이 셀의 파단을 구성/초기화 할 경우에주의 할 필요가있다.

+0

코딩 라인이 있습니다. 그러나 나는 한쪽에 이름과 다른쪽에 점수가있는 테이블보기에 그것을 어떻게 표시할지 모르겠습니다. 죄송합니다. 프로그래밍에 익숙하지 않습니다. – lol

+0

일부 샘플 테이블 뷰 컨트롤러 코드로 업데이트되었습니다. –

+0

코딩 해 주셔서 감사합니다. 하지만 어떻게 모든 배열에 대해 상수가 될 프레임을 설정합니까 – lol