第二个UipickerView不加载任何数据

时间:2014-03-11 06:55:39

标签: ios uipickerview

我在同一个ViewController中使用了两个UIPickerView。我需要将两个不同的数据列表加载到UIPickerView个。但我只能为uipickerView1而不是UIPickerView2

加载
    -(void)viewDidLoad{
      pickerView1.delegate=self;
      pickerView2.delegate=self;
      pickerView1.tag=1;
      pickerView2.tag=2;
      // Here I've loaded dataArray1 and dataArray2
    }

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



-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
    if(pickerView.tag==1)
    {
            return [dataArray1 count];
    }
    else if(pickerView.tag==1)
    {
        return [dataArray2 count];
    }

    return 1;
}


-(NSInteger)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView.tag==1)
    {
        if(row>0)
        {
        test1.text=[dataArray1 objectAtIndex:row];
        }else
        {
            test1.text=@"";
        }
    }
    else if(pickerView.tag==2)
    {
        if(row>0)
        {
            test2.text=[dataArray2 objectAtIndex:row];
        }
        else{
            test2.text=@"";
        }
    }

}

    -(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        NSString* title=@"";
        if(pickerView.tag==1)
        {
            title= [dataArray1 objectAtIndex:row];
        }
        else if (pickerView.tag==2)
        {
            title= [dataArray2 objectAtIndex:row];
        }
        return title;
    }

我已查看dataArray1dataArray2。两者都有5个数据。但是当加载viewController时,只有第一个选择器加载所有数据。对于第二个,只加载了一个在行索引为零的数据。这是什么错误?

1 个答案:

答案 0 :(得分:-1)

numberOfRowsInComponent方法

存在问题

您在两种情况下仅比较pickerView.tag==1

将其更改为

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

    if(pickerView.tag==1)
    {
            return [dataArray1 count];
    }
    else if(pickerView.tag==2)
    {
        return [dataArray2 count];
    }

    return 1;
}