UIPickerView委托方法titleForRow没有正确执行?

时间:2013-05-17 20:36:30

标签: ios uipickerview

伙计们在为UIPickerview实现委托方法时遇到了一个问题。问题的描述是,我想在pickerview中有3个组件,每个组件有不同的行数。其代码如下

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

}

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

{   
    NSInteger rows;
    if (0 == component)
        rows = 5;
    else if (1 == component)
        rows = 10;
    else
        rows = 15;

    NSLog(@"Number of rows returned for component %d are rows %d",component, rows);
    return rows;
}

在此之后我实现了“titleForRow”委托方法代码,其代码如下:

#pragma -mark UIPIckerView Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        //NSString *temp;


        //temp = [[NSString alloc] initWithString:[self.pocModelData.arrayOfDistricts objectAtIndex:row]];
    if (0 == component)
    {
    NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }
    else if
        (1 == component){
        NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }else{
        NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }
        //return temp;
        //  return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}

这个执行方法每个组件只返回3行,如下面的语句

所示
2013-05-18 02:00:40.743 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:41.221 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:41.696 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:42.035 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:43.761 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 0
2013-05-18 02:00:44.687 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 0
2013-05-18 02:00:45.048 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 0
2013-05-18 02:00:45.544 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:46.131 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 1
2013-05-18 02:00:46.620 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 1
2013-05-18 02:00:47.380 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 1
2013-05-18 02:00:47.772 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:48.543 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 2
2013-05-18 02:00:48.915 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 2
2013-05-18 02:00:49.461 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 2

我无法理解为什么每个组件只执行3次? 我使用一组数据填充行,因此数据源对于所有这些行都是通用的。这里的问题是什么,我现在无法掌握并坚持一段时间?任何指导都非常感谢并提前感谢。

1 个答案:

答案 0 :(得分:1)

它只查询可见的值。如果第0行位于微调器的中心,则可以看到3行。旋转后,它将查询其他值。