我如何显示保存在表中的数据

我有我的数据保存到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]; } 

为了使用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]来获得第i个元素。

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

这是你如何实现其中一种方法的例子:

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

希望能帮助到你 ;)

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