在2个不同的pickerviews中动态显示对象数组

时间:2010-10-13 10:19:02

标签: objective-c

你我正在使用pickerview但我不想简单地显示对象数组。我想要如果我在第一个选择器中选择一个对象,那么它应该动态更改并在其下方的第二个选择器中显示其对应的对象。 请帮忙.. 从2天开始我就被它困住了.... 感谢

2 个答案:

答案 0 :(得分:1)

enter code here
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
 {

    NSArray *pickerstrings;
NSArray *pickerstrings2;
NSMutableArray *data;
IBOutlet UIPickerView *pickerview1;

 }

@property (nonatomic, retain) UIPickerView *pickerview1;

//in RootViewController.m implementation file

 -(void)viewDidLoad 

{

[super viewDidLoad];

NSArray  *pickerstrings =[[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",nil];

NSArray  *pickerstrings2 = [[NSArray alloc] initWithObjects:@"Six",@"Seven",@"Eight",@"Nine",@"Ten",nil];


}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
 if(pickerView.tag==0) 

     return 2;

 else 
  {
     return 2;
  }

}

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

{

 if(pickerView.tag==0)
 {

   if(component ==0)
      return pickerstrings.count;
   else 
    {
      return pickerstrings2.count;
    } 

 }
else
{
  return data.count;
 }

}



-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row      
 forComponent:(NSInteger)component
{
   if(pickerView.tag==0)
   {
     if(component ==0)
     {
       return [pickerstrings objectAtIndex:row];
     }
    else
     {
       return [pickerstrings2 objectAtIndex:row];   
     }
  } 

  else 
  {
     if(component ==0)
     {
       return [data objectAtIndex:row];
     }
     else 
     {
       return[ data objectAtIndex:row];
     }
   }

 }

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

 {
   if(pickerView.tag==0)
   {

     data = [[[NSMutableArray alloc] init ]retain];
     UIPickerView *thePickerView2 = (UIPickerView*)[self.view viewWithTag:1];

     if(component==0)
     {

        for (int i=0; i<=row; i++)
        {
           [data addObject:[@"" stringByAppendingFormat:@"%d",i+1]];
        }

        [thePickerView2 reloadComponent:0];
        [thePickerView2 selectRow:row inComponent:0 animated:YES];
     }

     else
     {
        for(int j=6; j<=10; j++)
        {
           [data addObject:[@"" stringByAppendingFormat:@"%d", j]];
        }

       [thePickerView2 reloadComponent:1];
       [thePickerView2 selectRow:row inComponent:1 animated:YES];

     } 
    [data release];
  }

  else if(pickerView.tag==1)
  {
     if(component==0)
      {
        [pickerview1 selectRow:row inComponent:0 animated:YES];
      }
     else 
     {
       [pickerview1 selectRow:row inComponent:1 animated:YES];
     }
  }

}

答案 1 :(得分:0)

这很容易。看看这个实现。也许这有助于,如果没有,请随时提出。

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0)
        return 2;
    return 5;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        if (row == 0)
            return @"A";
        if (row == 1)
            return @"B";
    }
    if (component == 1)
        return [pickerArray objectAtIndex:row];
    return @"FOO";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        if (row == 0) {
            [pickerArray release];
            pickerArray = [[NSArray arrayWithObjects:@"Aa", @"Ab", @"Ac", @"Ad", @"Ae", nil] retain];
        }
        if (row == 1) {
            [pickerArray release];
            pickerArray = [[NSArray arrayWithObjects:@"Ba", @"Bb", @"Bc", @"Bd", @"Be", nil] retain];
        }
    }
    [pickerView reloadComponent:1];
}