我创建了一个UIPickerView
包含两个组件的组件,第一个组件 (A) 的固定行数设置为 13。另一个组件 (B) 的行数可变,取决于 (A) 中选择的行。
加载时,UIPickerView
我正在调用以下命令,以便我可以在两个组件中默认选择,但是我遇到的问题是只有组件 (A) 显示正确的值。组件 (B) 未显示正确的行集或正确的选择。
[picker selectRow:rowA inComponent:COMPONENT_A animated:YES];
[picker reloadAllComponents];
[picker selectRow:rowB inComponent:COMPONENT_B animated:YES];
I have tried printing out the following after calling this code and it seems that the correct values are printed out, yet when the picker shows only component (A) is showing the correct values and selection.
NSLog(@"(A) - row selected: %i", [picker selectedRowInComponent:COMPONENT_A]);
NSLog(@"(A) - number of rows: %i", [picker numberOfRowsInComponent:COMPONENT_A]);
NSLog(@"(B) - row selected: %i", [picker selectedRowInComponent:COMPONENT_B]);
NSLog(@"(B) - number of rows: %i", [picker numberOfRowsInComponent:COMPONENT_B]);
有没有人对如何调试这个或问题可能有任何想法?
更新
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case COMPONENT_A:
{
return 13;
}
case COMPONENT_B:
{
NSInteger selectedRowIdx = [picker selectedRowInComponent:COMPONENT_A];
switch (selectedRowIdx) {
case A: return 2;
case B: return 4;
case C: return 6;
case D: return 8;
case E: return 10;
case F: return 12;
case G: return 14;
case H: return 16;
case I: return 18;
case J: return 20;
case K: return 22;
case L: return 24;
default: return 1;
}
}
}
return -1;
}
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case COMPONENT_A:
{
switch (row) {
case A: return @"A";
case B: return @"B";
case C: return @"C";
case D: return @"D";
case E: return @"E";
case F: return @"F";
case G: return @"G";
case H: return @"H";
case I: return @"I";
case J: return @"J";
case K: return @"K";
case L: return @"L";
default: return @"";
}
}
case COMPONENT_B:
{
if (row == 0) {
return @"";
} else {
return [NSString stringWithFormat:@"%i", (int)row];
}
}
}
return nil;
}