2010-12-05 23 views
-1

我已经将数据保存到NSUserDefaults中,但我不知道如何将其显示到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:[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]; 
} 

回答

0

为了使用UITableView,您需要实现UITableViewDelegate和UITableViewDatasource方法。 (或者更好地说,你的UITableView需要一个数据源和委托,例如让你的视图控制器实现它们)

现在,你可能想要将所有行保存在一个数组中(NSArray/NSMutableArray)并使用NSUserDefaults保存孔阵列。 由于NSArray只接受NSObjects并且没有c结构体,为了将你的分数放到NSArray/NSMutableArray中,你需要另外一个你自己定义的类,或者你可以使用NSDictionary。这将是你的模型。 (记住MVC?))

一旦你有了这个,那么它很容易在UITableView中显示得分。

这是您的structArray转换成一个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]; 

的一种方法现在你只需要做[datasourceArray objectAtIndex:我],以获得第i个元素。

有了这个数组,你只需要按照UITableView的任何一个教程,this是我的谷歌搜索中的第一个。 ;) 或者,你可以看到许多苹果样品

这是你将如何实现的方法之一的例子:

- (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 

希望它帮助;)

注:上面的代码是大脑编译;)

相关问题