依赖 UIPickerView
有谁知道如何制作依赖 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这取决于您将如何保存数据。例如,如果您有一个数组作为字典键的值,并且该字典具有不同的此类键,则第一列将是键,选择一个数组后,您将在另一列(组件)。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
方法应返回 2。在
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
方法中,您需要给出组件 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
Then,
and
我确信这可以使用委托方法 pickerView:didSelectRow:inComponent: 和 reloadComponents 的组合来完成。
I'm sure this can be accomplished using a combination of the delegate method pickerView:didSelectRow:inComponent: and reloadComponents.
希望这有帮助,我有类似的情况,我的要求是当我在组件中选择国家/地区名称时,该国家/地区的城市应加载到第二个组件中。所以我所做的是,我有 2 个并排的选择器视图,当用户在国家/地区选择器中选择一行时,我使用适当的数据重新启动第二个选择器。
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.
您需要为第一个组件的每一行(索引为0)设置条件并打开一个开关来选择每个索引,我做到了并为我工作:
on ...
titleForRow:(NSinteger)row forComponent(NSInteger )组件
记住:你还必须这样做
- ...
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
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]
我认为,您喜欢在更改一个组件时更改另一个组件的相应项目。这也许是一个更好的解决方案。
代码:
注意:当您使用此代码时,组件值必须由两个 NSArray 保存,并且两个数组中相应的依赖值的顺序必须相同。
I think, you like to change the corresponding item of one component when change the other. It's maybe a better solution.
Code:
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.