未在didSelectRow上重新加载UIPickerView数组

时间:2011-01-09 18:42:42

标签: iphone objective-c cocoa-touch uikit uipickerview

让我试着解释一下我的观点以及我正在努力实现的目标。我有一个viewcontroller,里面有两个UIPickerView's。每个UIPickerView都通过IFB连接。一切都很好,但我试图在一个选择器上重新加载数据,具体取决于另一个选择器的选择。

.m文件

- (void)viewDidLoad
{
[super viewDidLoad];
pickerViewArray = [[NSMutableArray alloc] init];
[pickerViewArray addObject:@"Name 1"];
[pickerViewArray addObject:@"Name 2"];
[pickerViewArray addObject:@"Name 3"];
[pickerViewArray addObject:@"Name 4"];
[pickerViewArray addObject:@"Name 5"];

pickerViewArray2 = [[NSMutableArray alloc] init];
[pickerViewArray2 addObject:@"Phone 1"];
[pickerViewArray2 addObject:@"Phone 2"];
[pickerViewArray2 addObject:@"Phone 3"];
[pickerViewArray2 addObject:@"Phone 4"];
[pickerViewArray2 addObject:@"Phone 5"];
}
}

- (void)pickerView:(UIPickerView *)aPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  if([[pickerViewArray2 objectAtIndex:row] isEqual:@"Phone 2"]){
    [picker2 reload];
  }
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)aPickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)aPickerView numberOfRowsInComponent:(NSInteger)component {

    return [pickerViewArray count];

}

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

    return [pickerViewArray objectAtIndex:row];
}

编辑:解决问题我刚刚将数组添加到每个aPickerView,即titleForRow等。

1 个答案:

答案 0 :(得分:1)

重载方法就是这样做 - 重新加载现有数据。您需要将viewDidLoad中的逻辑移动到titleForRow和numberOfRowsInComponent中,这样一旦第一个选择器被操作,就可以返回正确的数据。目前,您只在viewDidLoad中将不同的内容加载到pickerViewArray2中,并且重新加载方法不会重新运行。