相关UIPickerView [英] Dependent UIPickerView
问题描述
任何一个是否知道如何使依赖UIPickerView。例如,当我选择组件之一,2行中的标题为组件两个转变?
Does any one know how to make dependent UIPickerView. For example when i select row 2 of component one the titles for component two change?
我看过,没有真正的答案在互联网上,我一直在使用if和switch语句,但他们只是试图崩溃
I have looked on the internet there is no real answer, i have tried using if and switch statements but they just crash.
推荐答案
这要看你如何去保留的数据。举一个例子,如果你有数组作为字典的键的值,并且该字典具有不同的这样的键,所述第一列将是键,并选择一你将被显示在其他列的阵列(组分)。 - (NSInteger的)numberOfComponentsInPickerView:(UIPickerView *)pickerView
方法应返回2。
在
- (NSInteger的)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger的)组件
方法,则需要给键的数量在字典组件1,并且当前选择的键的阵列的计数。
例如:
It depends on how you are going to keep data. For an example if you have an array as the value of a key of a dictionary, and that dictionary have different such keys, the first column will be the keys, and on selecting one you will be displaying the array in the other column (component). - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
method should return 2.
In
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
method, you need to give the number of keys in dictionary for component 1, and count of the array of the currently selected key.
eg
if(component==0) return [[DICTIONARY allKeys] count];
else return [[DICTIONARY objectForKey:@"SELECTED_KEY"] count];
然后,
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedIndex = [pickerView selectedRowInComponent:0];
if (component == 1 && !(selectedIndex < 0)) {
[pickerView reloadComponent:2];
[pickerView selectRow:0 inComponent:2 animated:YES];
}
}
和
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerRow = (view != nil) ? view : [[[UILabel alloc] initWithFrame:CGRectMake(5, 0, 115, 60)] autorelease];
pickerRow.font = [UIFont boldSystemFontOfSize:14];
pickerRow.textAlignment = UITextAlignmentLeft;
pickerRow.backgroundColor = [UIColor clearColor];
pickerRow.textColor = [UIColor blackColor];
pickerRow.numberOfLines = 0;
if (component == 0) {
pickerRow.text = @"DICTIONARY_ROW'th_KEY";
}
else {
pickerRow.text = [[dictionary objectForKey:@"SELECTED_KEY"] objectAtIndex:row];
}
return pickerRow;
}
这篇关于相关UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!