这可能是一个简单的问题,但是如何将多个组件添加到 UIPickerView?我使用 NSMutableArray 填充一个组件,但我不知道如何填充其他组件.选择一行时,我还需要更改标签的值.提前致谢基兰
It may be a simple question but how do I add multiple components to a UIPickerView? I use NSMutableArray to populate one component but I dont know how to populate the others. I also need to change the value of a label when a row is selected. Thanks in advance Kieran
我认为你是初学者.以下是如何实现 nacho 正确指出的方法:
I take it that you are a beginner. Here's how to implement the methods nacho has rightly pointed out:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// return the number of components required
return 2;
}
您可以使用其他 NSMutableArray
来填充组件.假设您有 2 个组件,每个组件使用不同的 NSMutableArray
即 array1
和 array2
:
You can use other NSMutableArray
's to populate the components. Assuming that you have 2 components, each using different NSMutableArray
i.e. array1
and array2
:
// Return row count for each of the components
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [array1 count];
}
else {
return [array2 count];
}
}
// Populate the rows of the Picker
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Component 0 should load the array1 values, Component 1 will have the array2 values
if (component == 0) {
return [array1 objectAtIndex:row];
}
else if (component == 1) {
return [array2 objectAtIndex:row];
}
return nil;
}
使用此代码更改 label
的文本,例如在 PickerView 中选择值时说 selectedValue
:
Use this code to change the text of a label
, say selectedValue
when a value is selected in the PickerView:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedValue.text = [NSString stringWithFormat: @"%@, %@", [array1 objectAtIndex:[myPicker selectedRowInComponent:0]],[array2 objectAtIndex:[myPicker selectedRowInComponent:1]]];
}
...你很高兴:)
您可能还想查看 http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ 获取有关 pickerView 编程的宝贵提示.
You might also want to see http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ for valuable tips on pickerView programming.
这篇关于如何将多个组件添加到 PickerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!