Question

I'm working on a custom PickerView, i have a two components in the picker (hours, minutes), i have made the "0" in the minutes component gray and not selectable, everything works fine except that the rows are getting reused... my Minutes component showing "0" in gray font (which is what i want) but if you scroll the picker, i see "7, 14, 21....." all in the gray font!! here is my code

enter code here
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

if(component == HOURS) {
        NSString *s = [self.arrayOfHours objectAtIndex:row];
        return [pickerView viewForShadedLabelWithText:s ofSize:20 forComponent:0 rightAlignedAt:52 reusingView:view];

    } else if (component == MINUTES) {
            NSString *s = [self.arrayOfMinutes objectAtIndex:row];
            return [pickerView viewForShadedLabelWithText:s ofSize:20 forComponent:1 rightAlignedAt:52 reusingView:view];
    }
    else return 0;
}


  - (UIView *)viewForShadedLabelWithText:(NSString *)title ofSize:(CGFloat)pointSize forComponent:(NSInteger)component rightAlignedAt:(CGFloat)offset reusingView:(UIView *)view {

//.........................    

    label = [self shadedLabelWithText:title ofSize:pointSize :component];

//..........................
 }



  - (UILabel *)shadedLabelWithText:(NSString *)label ofSize:(CGFloat)pointSize :(NSInteger)component 
{    

    UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];

    labelView.text = label;

    if([label isEqual:@"0"] && component == 1) {

        labelView.textColor = [UIColor grayColor];
    }

    return labelView;
    }

1 - Can someone please help me avoid picker view to reuse rows?

2 - How can i make the picker view show rows in round/circular ??

Was it helpful?

Solution

This is the code causing you trouble:

if([label isEqual:@"0"] && component == 1) {
    labelView.textColor = [UIColor grayColor];
}

You're problem is that, since the rows are getting reused, when you set one row to grey, the it goes off the stack, it will come back sometime. When it does come back, it's label color will still be grey.

So, the code should be changed to be like this:

if([label isEqual:@"0"] && component == 1) {
    labelView.textColor = [UIColor grayColor];
} else
{
    labelView.textColor = [UIColor blackColor]; // Or whatever color it normally is.
}

For your question about making it circular, you would set the number of rows to some crazy number like 10000 (something the user will never ever scroll through). Then set the picker's position to the middle of the 10K. You will then need to have an array that has all the values you want to display in your 'never-ending' picker.

You will then use the modulus operator (%) to check for the remainder of dividing your array's count by the current row. For example:

-(UIView *)somePickerWantsViewForRow:(int)row
{
    ...
    NSString *titleForRow = [self.someArray objectAtIndex:(self.someArray.count % row)];
    pickerRow.titleLabel.text = titleForRow;
}

In this example, the someArray might be ('cat','dog','farmer','pizza').

OTHER TIPS

I just figured out the "reused row" issue, it was as simple as replacing "view" with "nil" in the below method.

return [pickerView viewForShadedLabelWithText:s ofSize:20 forComponent:1 rightAlignedAt:52 reusingView:nil];

It took me couple of hours to figure this out.. wow, no wonder i love Programming! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top