如何将多个组件添加到PickerView?

时间:2010-09-27 18:03:04

标签: iphone arrays ios components uipickerview

这可能是一个简单的问题,但如何将多个组件添加到UIPickerView?我使用NSMutableArray来填充一个组件,但我不知道如何填充其他组件。我还需要在选择行时更改标签的值。 提前致谢 基兰

5 个答案:

答案 0 :(得分:10)

我认为你是初学者。以下是如何实现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;
}

使用此代码更改label的文字,例如在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编程的宝贵提示。

答案 1 :(得分:2)

您需要设置数据源并使用

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

设置其委托并使用:

– pickerView:didSelectRow:inComponent:

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

并且还可以看一眼documentation有助于:)

答案 2 :(得分:1)

而不是component我发现使用tag更合适。

enter image description here

然后在代码中:

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

答案 3 :(得分:1)

我不知道是否有点晚了但是我解决了另一种方式阵列有点简陋但是它可以创造奇迹你甚至可以为每个阵列添加不同的颜色

Call<ReponseBody> call = apiService.getDummieContent();
call.enqueue(...);

@Override
protected void onDestroy() {
    super.onDestroy();
    call.cancel();
}

在TextField中打印

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

}

答案 4 :(得分:1)

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

? E/dalvikvm: Could not find class 'com.longtailvideo.jwplayer.d.h', referenced from method com.longtailvideo.jwplayer.core.i.a
? E/dalvikvm: Could not find class 'com.longtailvideo.jwplayer.d.j', referenced from method com.longtailvideo.jwplayer.core.i.a
? E/dalvikvm: Could not find class 'com.longtailvideo.jwplayer.d.l', referenced from method com.longtailvideo.jwplayer.core.i.a