如何根据第一列中的行填充多个数组的UIPickerView列?

时间:2014-06-09 22:38:22

标签: ios uipickerview

我正在寻找与巴士相关的应用程序。我有一个带有两列的UIPickerView。第一列将列出所有总线。第二列将列出与第一列中选​​择的总线相对应的所有停靠点。无论在第一列中选​​择什么总线,列出每一个停靠点都是不明智的,所以我将停靠点分成数组(每个总线对应一个停靠点阵列)。

到目前为止,我已经让它只使用一个停止数组(stopsArray002)但是我不知道如何使它如果在第一列中选​​择了不同的总线(总线选择)它会自动填充第二列列)。

目前我有三个不同的停止阵列,每个阵列应对应于总线编号。我希望将总线标签设置为相应的总线并停止标签到相应的停止。截至目前,我的总线标签显示了我想要的内容(选择的总线),停止标签显示002停止。以下是我的代码。

ViewController.h

@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
    IBOutlet UILabel *busLabel;
    IBOutlet UILabel *stopLabel;
    IBOutlet UIPickerView *busPicker;

    NSArray *busArray;

    // STOP ARRAYS
    NSArray *stopsArray002;
    NSArray *stopsArray006;
    NSArray *stopsArray007;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    busArray = [[NSArray alloc] initWithObjects:
                @"BUS",
                @"002",
                @"006",
                @"007",
                @"012",
                @"014",
                @"017",
                @"101",
                @"102",
                @"103",
                @"141",
                @"142",
                @"147",
                @"181",
                @"182",
                @"189",
                @"241",
                @"300",
                @"301",
                @"302",
                @"303",
                @"304",
                @"401",
                @"402",
                @"403",
                @"500",
                @"501",
                @"502",
                @"503",
                @"640",
                @"701",
                @"702",
                @"703",
                @"704",
                @"819",
                @"940",
                nil];

    stopsArray002 = [[NSArray alloc] initWithObjects:
                     @"STOP",
                     @"002 1st",
                     @"002 2nd",
                     @"002 3rd",
                     @"002 4th",
                     @"002 5th",
                     @"002 6th",
                     @"002 7th",
                     nil];

    stopsArray006 = [[NSArray alloc] initWithObjects:
                     @"STOP",
                     @"006 1st",
                     @"006 2nd",
                     @"006 3rd",
                     @"006 4th",
                     nil];

    stopsArray007 = [[NSArray alloc] initWithObjects:
                     @"STOP",
                     @"006 1st",
                     @"006 2nd",
                     nil];
}

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

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0) // bus column
        return [busArray count];

    else // stop column
    {
        return [stopsArray002 count];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0)
        busLabel.text = [busArray objectAtIndex:row];

    else
        stopLabel.text = [stopsArray002 objectAtIndex:row];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
        return [busArray objectAtIndex:row];

    else
        return [stopsArray002 objectAtIndex:row];
}

@end

1 个答案:

答案 0 :(得分:0)

我会在这里使用字典,将数组添加到字典中,并使密钥成为总线的值。

所以当你想要一个特定总线的数组时,你会得到它像这样

NSMutableDictionary busStops
[busStops setObject:stopArray007 forKey:@"007"]
[busStops setObject:stopArray002 forKey:@"002"]

[busStops objectForKey:@"002"] //Will return array
[busStops allKeys] //Will return all your keys as an array you can use for first picker