UIPickerView - 使用多个数组填充1个选择器

时间:2012-09-07 12:10:01

标签: objective-c cocoa-touch uipickerview

我在Xcode 4.4中遇到了Picker View问题。我想用多个数组填充1 Picker。我观看/阅读了很多教程,几乎可以开始工作。这是我的代码:

*的.h

 #import <UIKit/UIKit.h>

@interface PickerView : UIViewController

<UIPickerViewDataSource,UIPickerViewDelegate>
{
    IBOutlet UIPickerView *picker;

    NSMutableArray *array_1;
    NSMutableArray *array_2;
}

@property (strong, nonatomic) IBOutlet UIView *pickerContainer;
@property (strong, nonatomic) IBOutlet UITextField *textfield_1;
@property (strong, nonatomic) IBOutlet UITextField *textfield_2;

- (IBAction)showArray_1Picker:(id)sender;
- (IBAction)showArray_2Picker:(id)sender;
- (IBAction)hidePicker:(id)sender;

@end

*。米

#import "PickerView.h"

@interface PickerView ()

@end

@implementation PickerView
@synthesize pickerContainer;
@synthesize textfield_1;
@synthesize textfield_2;

int textFieldTouched = 0;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent (NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 count];
            break;
        case 2:
            return [array_2 count];
            break;
        default:
            return 0;
    }
}


- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 objectAtIndex:row];
            break;
        case 2:
            return [array_2 objectAtIndex:row];
            break;
        default:
            return @"nothing";
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            textfield_1.text = [array_1 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
        case 2:
            textfield_2.text = [array_2 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
    }
}

 - (void)viewDidLoad
 {
    array_1 = [[NSMutableArray alloc] init];
    [array_1 addObject:@"1"];
    [array_1 addObject:@"2"];
    [array_1 addObject:@"3"];
    [array_1 addObject:@"4"];
    [array_1 addObject:@"5"];

    array_2 = [[NSMutableArray alloc] init];
    [array_2 addObject:@"a"];
    [array_2 addObject:@"b"];
    [array_2 addObject:@"c"];
    [array_2 addObject:@"d"];
    [array_2 addObject:@"e"];

    pickerContainer.frame = CGRectMake(0, 460, 320, 261);

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setPickerContainer:nil];
    [self setTextfield_1:nil];
    [self setTextfield_2:nil];
    [super viewDidUnload];
}

- (IBAction)showArray_1Picker:(id)sender
{
    [textfield_1 resignFirstResponder];
    textFieldTouched = 1;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)showArray_2Picker:(id)sender
{
    [textfield_2 resignFirstResponder];
    textFieldTouched = 2;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)hidePicker:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 460, 320, 261);
    [UIView commitAnimations];
}

@end

所以我遇到了两个主要问题:

  1. 如果我设置textFieldTouched = 0(或&gt; 2),就像我在我的选择器显示为空之前所做的那样。无论如何,如果我再次隐藏它,它会在文本字段中写入相应数组的第一个值。

  2. 如果我设置textFieldTouched = 1或= 2,则无论我尝试编辑哪个文本字段,都会使用相关数组填充选择器。但是,如果我将选择器上的值滚动到我的视线之外,当我向后滚动时它们会发生变化。

  3. 我想这样:

    如果我触摸文本字段1,则选择器将显示填充数组1

    如果我触摸文本字段2,则选择器将显示填充数组2

    等等。

    你可以帮帮我吗?

    我上传了代码,以便您可以更好地查看代码。

    https://dl.dropbox.com/u/5858884/ProblemCode.zip

    P.S。

    英语不是我母语所以请原谅错误;)

    我在两周前开始编程,所以如果你在我的代码中发现了一些不属于我的问题的错误请告诉我,这样我可以变得更好:)

1 个答案:

答案 0 :(得分:2)

在showArray1Picker和showArray2Picker方法中添加以下行:

[picker reloadAllComponents];

这将导致选择器重新加载其数据。您还需要将IBOutlet,选择器连接到IB中的选择器视图 - 我注意到它没有连接。