1

我现在有 2 个 UIPickerView,我想稍后再添加三个。代码如下:

-(void)viewDidLoad{
Pos_x = 5;
Pos_y = 70;

for (i= 0; i<2; i++) {

myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(Pos_x,Pos_y,150,300);
myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

if (i==0) {
// Init the data array.
Array_1 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_1 addObject:@"One"];
[Array_1 addObject:@"Two"];
[Array_1 addObject:@"Three"];
[Array_1 addObject:@"Four"];
[Array_1 addObject:@"Five"];
}

if (i==1) {

Array_2 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_2 addObject:@"One1"];
[Array_2 addObject:@"Two1"];
[Array_2 addObject:@"Three1"];
[Array_2 addObject:@"Four1"];
[Array_2 addObject:@"Five1"];  
}

Pos_x = Pos_x + 162;
}
}

我已经成功创建了两个带有标签的 UIPickerView。现在的问题是在 UIPickerView 中添加数据。

我正在这样做

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

//return [Array_1 objectAtIndex: row];  // If give only this line both UIPickerView show same value.

//How to show both 
//return [Array_2 objectAtIndex: row]; 

// I also tried this code

if(pickerView == myPickerView){

 if (i==0){
 }
 return [Array_1 objectAtIndex: row];
 }

 if (i==1){
 }
 return [Array_2 objectAtIndex: row];
 }


}

现在我的问题是如何在两个 UIPickerView 上显示不同的不同数据?就像在 pickerview.tag ==0 上一样,它会显示 return [Array_1 objectAtIndex: row]; 并且 pickerview.tag ==1 它会显示 return [Array_2 objectAtIndex: row];?欢迎任何想法或建议。

4

3 回答 3

2

请检查此代码:

-(void)viewDidLoad{
myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(5,70,150,300);
//myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

Array_1 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_1 addObject:@"One"];
[Array_1 addObject:@"Two"];
[Array_1 addObject:@"Three"];
[Array_1 addObject:@"Four"];
[Array_1 addObject:@"Five"];
[Array_1 addObject:@"Six"];


myPickerView_2nd = [[UIPickerView alloc] init];
myPickerView_2nd.frame = CGRectMake(167,70,150,300);
//myPickerView_2nd.tag = i;
myPickerView_2nd.delegate = self;
myPickerView_2nd.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView_2nd];

Array_2 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_2 addObject:@"One1"];
[Array_2 addObject:@"Two1"];
[Array_2 addObject:@"Three1"];
[Array_2 addObject:@"Four1"];
[Array_2 addObject:@"Five1"];
[Array_2 addObject:@"Six1"];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if([pickerView isEqual: myPickerView]){
// return the appropriate number of components, for instance
return 1;
}

if([pickerView isEqual: myPickerView_2nd]){
// return the appropriate number of components, for instance
return 1;
}
else 
return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {


if ( pickerView == myPickerView) // this is otherPickerview
{
    return [Array_1 count];


}
if ( pickerView == myPickerView_2nd){
    return [Array_2 count];


}
else{
     NSUInteger numRows = 5;
    return numRows;}

}

希望对你有帮助...

于 2013-02-20T09:46:18.093 回答
0

您可以使用数组或字典来存储内容数组并使用选取器的标签获取它们。例如。

NSArray *contentArrays = [[NSArray alloc] initWithObjects:Array_1, Array_2, nil];

然后获取内容数组 [contentArrays objectAtIndex:pickerView.tag];

于 2013-02-20T09:09:24.610 回答
0

您可以通过设置pickerview的标签来做到这一点:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) { 
    if(pickerView.tag == 1)
     {
          // do something with picker one
     }
     else if(pickerView.tag == 2)
     {
          // the other picker
     }
}

要不然:

   - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) { 
        switch(pickerView.tag){
           case 1:
               //do something
               break;
           case 2:
               //do other thing
               break;
           default:
               break;
        }
    }

希望这可以帮助..

也看看这个:Multiple PickerViews in one View?

于 2013-02-20T09:22:10.473 回答