依赖 UIPickerView

发布于 2024-10-21 22:08:39 字数 120 浏览 2 评论 0原文

有谁知道如何制作依赖 UIPickerView.例如,当我选择组件一的第 2 行时,组件二的标题会发生变化吗?

我在互联网上查看过,没有真正的答案,我尝试过使用 if 和 switch 语句,但它们只是崩溃了。

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?

I have looked on the internet there is no real answer, i have tried using if and switch statements but they just crash.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

櫻之舞 2024-10-28 22:08:39

这取决于您将如何保存数据。例如,如果您有一个数组作为字典键的值,并且该字典具有不同的此类键,则第一列将是键,选择一个数组后,您将在另一列(组件)。 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 方法应返回 2。

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
方法中,您需要给出组件 1 的字典中键的数量,以及当前所选键的数组的数量。
例如

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

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

Then,

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

}

and

- (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;
}
柳若烟 2024-10-28 22:08:39

我确信这可以使用委托方法 pickerView:didSelectRow:inComponent: 和 reloadComponents 的组合来完成。

I'm sure this can be accomplished using a combination of the delegate method pickerView:didSelectRow:inComponent: and reloadComponents.

妄司 2024-10-28 22:08:39

希望这有帮助,我有类似的情况,我的要求是当我在组件中选择国家/地区名称时,该国家/地区的城市应加载到第二个组件中。所以我所做的是,我有 2 个并排的选择器视图,当用户在国家/地区选择器中选择一行时,我使用适当的数据重新启动第二个选择器。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    if (pickerView == countryPickerView)    
    {

        if(PickerViewCity)
        {
            [PickerViewCity removeFromSuperview];
            [PickerViewCity release];
            PickerViewCity=nil;
        }


        PickerViewCity = [[UIPickerView alloc] initWithFrame:CGRectMake(160, 38, 320, 240)];
        PickerViewArrayCity=[[countryPickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]]objectForKey:@"nodeChildArray"];
        //NSLog(@"%@",PickerViewArrayCity);
        PickerViewCity.showsSelectionIndicator = YES;   // note this is default to NO
        PickerViewCity.delegate = self;
        PickerViewCity.dataSource = self;
        CGAffineTransform s20 = CGAffineTransformMakeScale(0.5, 0.58);
        CGAffineTransform t21 = CGAffineTransformMakeTranslation(-80,-50);
        PickerViewCity.transform = CGAffineTransformConcat(s20, t21);
        PickerViewCity.layer.masksToBounds = YES;
        [someview addSubview:PickerViewCity];       
    }


    if (pickerView == PickerViewCity)   
    {


    }   
}


Hope this helps, I had a similar situation, my requirement was when i select a country name in a component, the cities of the country should be loaded in the second component. So what i did was, i had 2 pickerview side by side and when the user selects a row in country picker i re-initiated the second picker with appropriate data.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    if (pickerView == countryPickerView)    
    {

        if(PickerViewCity)
        {
            [PickerViewCity removeFromSuperview];
            [PickerViewCity release];
            PickerViewCity=nil;
        }


        PickerViewCity = [[UIPickerView alloc] initWithFrame:CGRectMake(160, 38, 320, 240)];
        PickerViewArrayCity=[[countryPickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]]objectForKey:@"nodeChildArray"];
        //NSLog(@"%@",PickerViewArrayCity);
        PickerViewCity.showsSelectionIndicator = YES;   // note this is default to NO
        PickerViewCity.delegate = self;
        PickerViewCity.dataSource = self;
        CGAffineTransform s20 = CGAffineTransformMakeScale(0.5, 0.58);
        CGAffineTransform t21 = CGAffineTransformMakeTranslation(-80,-50);
        PickerViewCity.transform = CGAffineTransformConcat(s20, t21);
        PickerViewCity.layer.masksToBounds = YES;
        [someview addSubview:PickerViewCity];       
    }


    if (pickerView == PickerViewCity)   
    {


    }   
}


黑凤梨 2024-10-28 22:08:39

您需要为第一个组件的每一行(索引为0)设置条件并打开一个开关来选择每个索引,我做到了并为我工作:

on ...titleForRow:(NSinteger)row forComponent(NSInteger )组件

{
    if (component == 0)  // this is first component from left to right
        return [arrayForFirstComponent objectAtIndex:row];
    if (component == 1)  // this is second component from left to right
        switch([pickerIBOutletAsYouNamedIt selectedRowInComponent:0])  // here you set content of component two depending on which row is selected from component 1
        {
             case 0:            // remember index starts at 0
                 return [arrayForComponent2ForRow1inComponent1 objectAtIndex:row];
                 break;
             //... and so on for each row in the component 1 (with index 0)
        }
}

记住:你还必须这样做
- ...numberOfRowsInComponent:(NSInteger)组件
并与
- ...didSelectRow:(NSInteger)行 inComponent:(NSInteger)组件
在他的最后一个中,在选择组件 0 处的行时,调用 [pikcerIBOutlet reloadComponent:1]

you need to set conditions for each row of the first component (with index 0) and open a switch to select each index, I did it and worked for me:

on ...titleForRow:(NSinteger)row forComponent(NSInteger)component

{
    if (component == 0)  // this is first component from left to right
        return [arrayForFirstComponent objectAtIndex:row];
    if (component == 1)  // this is second component from left to right
        switch([pickerIBOutletAsYouNamedIt selectedRowInComponent:0])  // here you set content of component two depending on which row is selected from component 1
        {
             case 0:            // remember index starts at 0
                 return [arrayForComponent2ForRow1inComponent1 objectAtIndex:row];
                 break;
             //... and so on for each row in the component 1 (with index 0)
        }
}

remember: YOU HAVE TO DO THIS ALSO WITH
- ...numberOfRowsInComponent:(NSInteger)component
and with
- ...didSelectRow:(NSInteger)row inComponent:(NSInteger)component
and in his last one, in selection of row at component 0 call [pikcerIBOutlet reloadComponent:1]

贱人配狗天长地久 2024-10-28 22:08:39

我认为,您喜欢在更改一个组件时更改另一个组件的相应项目。这也许是一个更好的解决方案。

代码:

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
    if (component == 0) {

        //Here First Part of the Pickerview is represented using 0.
        // dependentPicker is the Pickerview outlet

        [dependentPicker selectRow:row inComponent:1 animated:YES];
        [dependentPicker reloadComponent:1];
    }
    else
    {
        [dependentPicker selectRow:row inComponent:0 animated:YES];
        [dependentPicker reloadComponent:1];
    }
}

注意:当您使用此代码时,组件值必须由两个 NSArray 保存,并且两个数组中相应的依赖值的顺序必须相同。

I think, you like to change the corresponding item of one component when change the other. It's maybe a better solution.

Code:

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
    if (component == 0) {

        //Here First Part of the Pickerview is represented using 0.
        // dependentPicker is the Pickerview outlet

        [dependentPicker selectRow:row inComponent:1 animated:YES];
        [dependentPicker reloadComponent:1];
    }
    else
    {
        [dependentPicker selectRow:row inComponent:0 animated:YES];
        [dependentPicker reloadComponent:1];
    }
}

Note: When you use this code the component values must be Hold by two NSArray and the order of the corresponding dependent values must be same in both arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文