麻烦的双(双组件)选择器视图

时间:2012-02-08 19:14:42

标签: ios uipickerview

我正在使用双(双组件)选择器视图。当我运行它时,向我显示双选择器视图,但两个组件的行中都有相同的信息。

以下代码希望你能帮助我。
我想在第一个组件中显示array1,在第二个组件中显示array2。

- (void)viewDidLoad {
    NSArray *FirstArray1 = [[NSArray alloc] initWithObjects:
      @"White",@"Whole Wheat",@"Rye",@"Sourdough",@"Seven Grain", nil];
    self.Array1 = FirstArray1;

    NSArray *FirstArray2 = [[NSArray alloc] initWithObjects:
      @"Turkey",@"Peanut Butter",@"Tuna Salad",@"Chicken Salad",@"Roast Beef",
      @"Vegemite",nil];
    self.Array2 = FirstArray2;

    [super viewDidLoad];
 }

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

-(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component
{
    return[self.Array1 count];
    return[self.Array2 count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
    forComponent:(NSInteger)component 
{
    return [self.Array1 objectAtIndex:row];
    return [self.Array2 objectAtIndex:row];
}

2 个答案:

答案 0 :(得分:2)

-(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
    return[self.Array1 count];
} else if (component == 1) {
    return[self.Array2 count];
} 
return 0;
}

答案 1 :(得分:1)

这就是我喜欢管理事物的方式......

let pickerData = [["column1_1","column1_2","column1_3","column1_4"],
                  ["column2_1","column2_2","column2_3","column2_4"]]

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

组件编号引用数组,简洁明了。