使用带有更改值的pickerview的JSON

时间:2014-04-24 10:39:55

标签: ios json uipickerview

我对Xcode和Obj-C编程仍然很陌生,所以请提前道歉。

使用这个网站和其他一些网站,我设法将一个带有两个组件的pickerview拼凑在一起,在选择第一个组件中的一个选项后,第二个组件会自动更改,以显示与第一个组件中所选选项相关的选项。 我工作的示例选择器视图使用这些国家/地区的国家和足球俱乐部,例如如果您在第一个组件中选择英格兰,则会在第二个组件中获得英语团队列表,如果您在第一个组件中选择西班牙,则会获得第二个组件中的西班牙团队列表等。

目前,所有这些国家和团队都在Xcode中进行了硬编码。 我要做的是将这些县和团队存储在MySQL数据库中,然后使用php将这些记录发送到应用程序。之后我想在数组中使用这些json记录并在选择器视图中显示它们。

将json数据导入应用程序时没有问题,但是当在应用程序上使用带有选择器视图的json时,我遇到了一些问题。我没有运气让选择器视图与json数组一起工作,它开始变得非常令人沮丧

以下是我目前与硬编码县和团队签订的挑选者视图的代码:

#import "ViewController.h"

@interface ViewController ()
{
NSMutableArray *Nations;
NSMutableArray *England;
NSMutableArray *Espana;
NSMutableArray *Netherlands;
NSMutableArray *Germany;
NSMutableArray *Italy;

// Define keys
NSString *club;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Nations = [[NSMutableArray alloc]initWithObjects:@"England",@"Espana",@"Netherlands",@"Germany",@"Italy", nil];

England=[[NSMutableArray alloc]initWithObjects:@"Arsenal",@"Chelsea",@"Manchester City",@"Manchester United",@"Liverpool",@"Tottenham",@"Fulham City",@"Stoke City",@"Sunderland",@"NewCastle United",@"Blackburn Rovers",@"Southampton",@"Wolvers",@"Aston Villa", nil];

Espana = [[NSMutableArray alloc]initWithObjects:@"Barcelona",@"Real Madrid",@"Valencia",@"Athletico Madrid",@"Athletico Balbao",@"Getafe CF",@"Sevilla CF", nil];

Netherlands = [[NSMutableArray alloc]initWithObjects:@"Celtics",@"Ajax",@"Amesterdam", nil];

Germany = [[NSMutableArray alloc]initWithObjects:@"Bayern Munich",@"Bermen",@"Fiorentina",@"Pampas",@"Nord", nil];

Italy = [[NSMutableArray alloc]initWithObjects:@"AC Milan",@"Inter Milan",@"Juventus", nil];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component ==0)
    {
        return [Nations count];
    }
    else {
        if ([club isEqualToString:@"Espana"]) {
            return [Espana count];
        }
        if ([club isEqualToString:@"Germany"]) {
            return [Germany count];
        }
        if ([club isEqualToString:@"Netherlands"]) {
            return [Netherlands count];
        }
        if ([club isEqualToString:@"Italy"]) {
            return [Italy count];
        }
        else  {
            return [England count];
        }
    }

    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:    (NSInteger)component
{
    if(component ==0)
    {
        return [Nations objectAtIndex:row];
    }
    else {
        if ([club isEqualToString:@"Espana"]) {
            return [Espana objectAtIndex:row];
        }
        if ([club isEqualToString:@"Germany"]) {
            return [Germany objectAtIndex:row];
        }
        if ([club isEqualToString:@"Netherlands"]) {
           return [Netherlands objectAtIndex:row];
        }
        if ([club isEqualToString:@"Italy"]) {
            return [Italy objectAtIndex:row];
        }
        else  {
            return [England objectAtIndex:row];


        }
    }

    return 0;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
        [pickerView reloadComponent:1];
    }
 }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 - (void)dealloc {
}

@end

2 个答案:

答案 0 :(得分:0)

到底发生了什么?你的代码看起来非常好。

您是否设置了UIPickerView

viewDidLoad中,它应如下所示:

UIPickerView * mainPickerView = [[UIPickerView alloc] init];
[mainPickerView setShowsSelectionIndicator:YES];
mainPickerView.backgroundColor = [UIColor whiteColor];
mainPickerView.dataSource = self;
mainPickerView.delegate = self;

确保datasourcedelegate已设置。还要确保你有合适的框架。总的来说,如果您的[mainPickerView reloadAllComponents];可见,请务必在从服务器获取信息后致电UIPickerView

随意提问。

所以你基本上使用一些网络框架来获取你的JSON服务器答案,其中90%的时间是NSDictionary,如下所示:

NSDictionary *result=[NSJSONSerialization JSONObjectWithData:resultData options:kNilOptions error:&error];

在该词典中,您有一些实际上NSArrays的对象。只需将现有的NSArrays替换为UIPicker,重新加载{{1}},就可以了。

答案 1 :(得分:0)

这令人沮丧,因为很难跟踪不同的阵列。 我建议您将数据结构更改为NSDictionary词典。 你的JSON应该看起来一样。

<强> viewDidLoad中

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


self.listOfclubs = 
@{
 @"Nations ":@[@"England",@"Espana",@"Netherlands",@"Germany",@"Italy"]
 @"England":@[@"Arsenal",@"Chelsea",@"Manchester City",@"Manchester United",@"Liverpool",@"Tottenham",@"Fulham City",@"Stoke City",@"Sunderland",@"NewCastle United",@"Blackburn Rovers",@"Southampton",@"Wolvers",@"Aston Villa"],
 @"Espana ":@[@"Barcelona",@"Real Madrid",@"Valencia",@"Athletico Madrid",@"Athletico Balbao",@"Getafe CF",@"Sevilla CF"],
 @"Netherlands":@[@"Celtics",@"Ajax",@"Amesterdam"],
 @"Germany  ":@[@"Bayern Munich",@"Bermen",@"Fiorentina",@"Pampas",@"Nord"],
 @"Italy  ":@[@"AC Milan",@"Inter Milan",@"Juventus"],
};
}

<强> numberOfRowsInComponent

- (NSInteger)pickerView:(UIPickerView *)pickerView 
                                      numberOfRowsInComponent:(NSInteger)component
{
    return [listOfclubs objectForKey:club];
}

<强> titleForRow

- (NSString *)pickerView:(UIPickerView *)pickerView 
                     titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[listOfclubs objectForKey:club] objectAtIndex:row];
}

看看这段代码与之前的代码相比有多么简单和灵活。