Search code examples
iphoneios5uipickerviewpicker

For two pickers in a single view, titleForRow always gets called with component value 0


I have two pickers in a singe view controller. They use two NSArray for their data.

The problem is that for both pickers, when the titleForRow method is called, the value of the component parameter is always zero, hence all the rows display the first string from their data arrays.

Why is this happening and how do i fix it?

These are my delegate methods:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    NSLog(@"%d",[pickerView tag]);
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component
{
    NSLog(@"%d",[pickerView tag]);
    if ([pickerView tag] == 0)
        return providersList.count;
    else
        return amountList.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSLog(@"%d, %d",[pickerView tag],component);
    if ([pickerView tag] == 0)
        return [providersList objectAtIndex:component];
    else
        if ([pickerView tag] == 1)
            return [amountList objectAtIndex:component];
        else
            return nil;
}

Using xcode 4.3 and iOS5.


Solution

  • Component is used for number of sections in picker and row is used for a particular row in component. When your picker is having multiple sections, you will receive different values for component. Row is indicating the Row in component.

    So, if you have only one component then you should use row.

    [amountList objectAtIndex:row];