0

我遇到了两个 UIPickerViews 的问题。

第一个名为 pickerParent 的选择器选择一种食物。第二个名为pickerChild 的选择器会显示这些食物类型的列表。

问题是如果pickerChild 尝试选择其中一项,pickerChild 然后会更新自身以匹配pickerParent 列表中的相应项目。(例如:如果您在 pickerParent 上选择肉类,pickerChild 会显示“Ginger Beef, Lemon Chicken, Salt & Pepper Ribs, and Sweet & Sour Pork”。如果您随后在 pickerChild 列表中选择 Sweet & Sour Pork,则为第四项列表,它就会获得蔬菜的属性,并且只显示“什锦蔬菜”)

有人可以帮我这样pickerChild不会更新自己吗?

我的代码如下。

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MainPage () {
   NSMutableArray *_objects;
}
@end

@implementation MainPage
@synthesize pickerChild;
@synthesize pickerParent;
@synthesize image_view;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return ( pickerView == pickerParent ? 1 : 1 );
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSArray *values = ( pickerView == pickerParent ? topics : items );
    return [values count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSArray *values = ( pickerView == pickerParent ? topics : items );
    return [values objectAtIndex: row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if ([[topics objectAtIndex:row] isEqual:@"Noodles"]) {
       items = noodle;
    } else if ([[topics objectAtIndex:row] isEqual:@"Rice"]) 
    {
       items = rice;
    } else if ([[topics objectAtIndex:row] isEqual:@"Meat"]) 
    {
        items = meat;
    } else if ([[topics objectAtIndex:row] isEqual:@"Vegetables"]) 
    {
        items = vegatable;
    } else if ([[topics objectAtIndex:row] isEqual:@"Favorites"]) 
    {
        items = favorite;
    }
[pickerChild reloadAllComponents];

}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    topics = [[NSMutableArray alloc] init];
    [topics addObject:@"Noodles"];
    [topics addObject:@"Rice"];
    [topics addObject:@"Meat"];
    [topics addObject:@"Vegetables"];
    [topics addObject:@"Favorites"];

    noodle = [[NSMutableArray alloc] init];
    [noodle addObject:@"Fried Noodles"];
    [noodle addObject:@"Shanghai Noodles"];

    meat = [[NSMutableArray alloc] init];
    [meat addObject:@"Ginger Beef"];
    [meat addObject:@"Lemon Chicken"];
    [meat addObject:@"Salt & Pepper Ribs"];
    [meat addObject:@"Sweet & Sour Pork"];

    vegatable = [[NSMutableArray alloc] init];
    [vegatable addObject:@"Assorted Vegetables"];

    rice = [[NSMutableArray alloc] init];
   [rice addObject:@"Fried Rice"];

    items = noodle;

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}
@end
4

1 回答 1

0

您的didSelectRow..委托方法不区分两个选择器,因此无论您更改了哪个选择器,您的子选择器都会重新加载。如果父选择器中的选择已更改,您只想重新加载子选择器。

因此,将您当前拥有的代码包装在:

 if (pickerView == parentPicker)

你应该没事。

于 2012-05-31T22:25:29.910 回答