如何向 PickerView 添加多个组件?

发布于 2024-09-24 23:44:02 字数 113 浏览 5 评论 0原文

这可能是一个简单的问题,但如何将多个组件添加到 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

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

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

发布评论

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

评论(5

逆蝶 2024-10-01 23:44:02

我认为你是一个初学者。以下是如何实现 nacho 正确指出的方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    // return the number of components required
    return 2;
}

您可以使用其他 NSMutableArray 来填充组件。假设您有 2 个组件,每个组件使用不同的 NSMutableArray,即 array1array2

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

使用此代码更改 标签的文本,在 PickerView 中选择一个值时说 selectedValue

- (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 编程的宝贵提示。

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

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

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

... and you are good to go :)

You might also want to see http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ for valuable tips on pickerView programming.

禾厶谷欠 2024-10-01 23:44:02

您需要设置数据源并使用

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

设置其委托并使用:

– pickerView:didSelectRow:inComponent:

在选择特定组件中的特定行时更改标签。很可能是 UITableViewDelegate 和 Datasource

,并且还浏览了

You need to set the datasource and use

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

set its delegate and use:

– pickerView:didSelectRow:inComponent:

to change your label when a certain row in a certain component is selected. Very likely to UITableViewDelegate and Datasource

and also taking a glance to documentation helps :)

怪异←思 2024-10-01 23:44:02

我发现使用 tag 代替 component 更合适。

在此处输入图像描述

然后在代码中:

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //NSLog(@"Component_1: %d",pickerView.tag);
    if (pickerView.tag == 1)
        return [self.arrOpponentTeams count];
    else if (pickerView.tag == 2)
        return [self.arrMyTeams count];
    else
        return [self.arrPlayers count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //NSLog(@"Component_2: %d",pickerView.tag);
    if( pickerView.tag == 1 )
        return [self.arrOpponentTeams objectAtIndex:row];
    else if (pickerView.tag == 2)
        return [self.arrMyTeams objectAtIndex:row];
    else
        return [self.arrPlayers objectAtIndex:row];
}

Instead of component I found using tag more proper.

enter image description here

Then in the code:

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //NSLog(@"Component_1: %d",pickerView.tag);
    if (pickerView.tag == 1)
        return [self.arrOpponentTeams count];
    else if (pickerView.tag == 2)
        return [self.arrMyTeams count];
    else
        return [self.arrPlayers count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //NSLog(@"Component_2: %d",pickerView.tag);
    if( pickerView.tag == 1 )
        return [self.arrOpponentTeams objectAtIndex:row];
    else if (pickerView.tag == 2)
        return [self.arrMyTeams objectAtIndex:row];
    else
        return [self.arrPlayers objectAtIndex:row];
}
娜些时光,永不杰束 2024-10-01 23:44:02

我不知道是否有点晚了但我用另一种方式解决了这个问题数组有点简陋但它的效果很神奇你甚至可以为每个数组添加不同的颜色

- (void) viewDidLoad {
  horPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..24];
  minPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..59];
  secPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..59];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;   
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
        return _HorPickerArray.count ;
    }
    else if (component == 1) {
        return _MinPickerArray.count ;
    }
    else if (component == 2) {
        return _SecPickerArray.count ;
    }
}

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
        return [_HorPickerArray objectAtIndex:row];
    }
    else if (component == 1) {
        return [_MinPickerArray objectAtIndex:row];
    }
    else if (component == 2) {
        return [_SecPickerArray objectAtIndex:row];
    }
    return nil;

}

并在 TextField 中打印

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

    _OficialTime.text  =[NSString stringWithFormat: @"%@:%@:%@",    [_HorPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:0]],[_MinPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:1]],  [_SecPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:2]]]; 
}

I do not know if it's a little late But I solved this another way Array are a bit rudimentary But it works wonders You can even put different color to each array

- (void) viewDidLoad {
  horPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..24];
  minPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..59];
  secPickerArray = @[@"00",@"01",@"02",@"03",@"04",@"05".to..59];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;   
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
        return _HorPickerArray.count ;
    }
    else if (component == 1) {
        return _MinPickerArray.count ;
    }
    else if (component == 2) {
        return _SecPickerArray.count ;
    }
}

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
        return [_HorPickerArray objectAtIndex:row];
    }
    else if (component == 1) {
        return [_MinPickerArray objectAtIndex:row];
    }
    else if (component == 2) {
        return [_SecPickerArray objectAtIndex:row];
    }
    return nil;

}

And Print in TextField

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

    _OficialTime.text  =[NSString stringWithFormat: @"%@:%@:%@",    [_HorPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:0]],[_MinPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:1]],  [_SecPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:2]]]; 
}
待"谢繁草 2024-10-01 23:44:02

这是 swift3 代码。这里我将多个选择器视图的值分配给 UITextField。

var pickOption = [["1", "2", "3","4","5","6","7","8","9"], ["0","1","2", "3","4","5","6"],["0","1","2", "3","4","5","6"]]
@IBOutlet weak var travellersTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    let pickerView = UIPickerView()
    pickerView.delegate = self
    travellersTextField.inputView = pickerView
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return pickOption.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickOption[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return  pickOption[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let adult = pickOption[0][pickerView.selectedRow(inComponent: 0)]
    let child = pickOption[1][pickerView.selectedRow(inComponent: 1)]
    let infant = pickOption[2][pickerView.selectedRow(inComponent: 2)]

    travellersTextField.text = adult + " adults," + child + " children," + infant + " infants"
    travellersTextField.becomeFirstResponder()

}

Here is the swift3 code.Here I am assigning values of multiple picker view to a UITextField.

var pickOption = [["1", "2", "3","4","5","6","7","8","9"], ["0","1","2", "3","4","5","6"],["0","1","2", "3","4","5","6"]]
@IBOutlet weak var travellersTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    let pickerView = UIPickerView()
    pickerView.delegate = self
    travellersTextField.inputView = pickerView
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return pickOption.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickOption[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return  pickOption[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let adult = pickOption[0][pickerView.selectedRow(inComponent: 0)]
    let child = pickOption[1][pickerView.selectedRow(inComponent: 1)]
    let infant = pickOption[2][pickerView.selectedRow(inComponent: 2)]

    travellersTextField.text = adult + " adults," + child + " children," + infant + " infants"
    travellersTextField.becomeFirstResponder()

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