带条件的UIPickerView

时间:2012-09-25 16:27:57

标签: ios uipickerview

我对Xcode完全陌生并且与编码有关的任何事情,但我正在向它发展并且似乎直到现在才行。我想做什么......

我有一个UIPickerView,它有两个由数组填充的组件。它很好,工作可爱,但我希望更进一步。

在左侧组件上,我希望列表中的常量值永远不会改变。在第二个组件中,我希望有一个在第一个组件中单击的任何选择的子列表。

例如: -

BOY : Matt
      Tom
      Pete

GIRL : Jess
       Nina
       Sarah

我认为我可以使用if语句来实现这些更改,但不知道语言......一个人可以尝试这么多的试验和错误!

干杯

@interface TripsViewController ()
@end

@implementation TripsViewController

@synthesize ActivitysLabel;
@synthesize TripsLabel;

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == ACTIVITY)
        return [ArrayActivity count];

    if (component == TRIPS)
        return [ArrayTrips count];

    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (component == ACTIVITY)
        return [ArrayActivity objectAtIndex:row];

    if (component == TRIPS)
        return [ArrayTrips objectAtIndex:row];
    return 0;
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    ActivitysLabel.text = [ArrayActivity objectAtIndex:[pickerView selectedRowInComponent:0]];
    TripsLabel.text = [ArrayTrips objectAtIndex:[pickerView selectedRowInComponent:1]];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad

{
    [super viewDidLoad];
    ArrayActivity = [[NSMutableArray alloc] init];
    [ArrayActivity addObject:@"Abseiling"];
    [ArrayActivity addObject:@"Canyoning"];
    [ArrayActivity addObject:@"Rock Climbing"];
    [ArrayActivity addObject:@"Bush Walking"];
    [ArrayActivity addObject:@"Mountain Biking"];
    [ArrayActivity addObject:@"4wd Tours"];


    // My attempts at trying to solve this problem


    if (ArrayActivity containsObject:NSString @"Canyoning") {
        ArrayTrips = [[NSMutableArray alloc] init];
        [ArrayTrips addObject:@"Empress Canyon"];
        [ArrayTrips addObject:@"Sheep Dip"];
        [ArrayTrips addObject:@"butterbox"];
    } 

1 个答案:

答案 0 :(得分:0)

我认为代码通常是正确的,但是在选择将改变可能选择的项目之后你必须做的是使用数据不同的列的索引调用reloadComponent。在选择新条目后立即执行此操作(确保仅在正确的组件选择上触发重新加载)。