开发者

UIPickerView's Array is not being reloaded upon didSelectRow

Let me try to explain what I have on view and what's I'm trying to accomplish. I have 开发者_开发问答a viewcontroller with two UIPickerView's in it. Each UIPickerView is connected via IFB. Everything works great but I'm trying to reload the data on one picker depending on which selection of another picker.

.m file

- (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];
}

Edit: to fix the problem I was having just add the array to each aPickerView i.e. titleForRow etc.


The reload method does just that - reloads the existing data. You need to move the logic that you have in viewDidLoad into titleForRow and numberOfRowsInComponent so that once the first picker has been actioned, the correct data can be returned. At the moment you load something different into pickerViewArray2 only at viewDidLoad, and that won't be re-run by your reload method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜