Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何显示表中保存的数据_Ios_Iphone - Fatal编程技术网

Ios 如何显示表中保存的数据

Ios 如何显示表中保存的数据,ios,iphone,Ios,Iphone,我已将数据保存到NSUserDefaults中,但不知道如何将其显示到UITable中 -(IBAction)savehighscore_button { int i, ii = -1; struct high_score { NSString *name; int highScore; }; NSString *nameentered; nameentered = nametextbox.text; s

我已将数据保存到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];
}
-(iAction)保存高分按钮{
int i,ii=-1;
结构高分{
NSString*名称;
int高分;
}; 
输入NSString*名称;
nameentered=nametextbox.text;
struct high_score structArray[10];
NSUserDefaults*userPreferences=[NSUserDefaults standardUserDefaults];
对于(i=0;i0){
对于(i==ii;i>=0;i--){
if(myScore>structArray[i].highScore){

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

现在,您可能不希望为每一行使用不同的键,而是希望将所有行保存在一个数组(NSArray/NSMutableArray)中,并使用NSUserDefaults保存孔数组。 由于NSArray只接受NSObject而不接受NSC结构,要将分数放入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];
希望有帮助;)

注:以上代码为brain编译;)

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