views:

2584

answers:

5

Ok, maybe I'm missing something really simple and I apologize if that's the case, however, I've googled every permutation of the title and have not found! So this is simply what I want to do: change the background color of the label I'm using as the row view in a 2 component pickerview when that row has been selected. So I thought this would work:

if (row == [pickerview selectedRowForComponent])
    viewlabel.backgroundColor = redcolor;

but this doesn't work. It seems to arbitrarily choose which row to color and sometimes even give a bad access error. I've tried all different clauses to no effect! ANy suggestions would be greatly appreciated!!

Here's the full method:

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

    if (component == kNumberComponent) {


#define PICKER_LABEL_FONT_SIZE 24
#define PICKER_LABEL_ALPHA 1.0
     // UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
     UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:24];
     UILabel *carsLabel =[ [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 50) ]autorelease];
     //[picker selectRow:row inComponent:component animated:YES];
     NSString *pickerText = [self.numbers objectAtIndex:(int)row];
     carsLabel.text = pickerText;
     carsLabel.textAlignment = UITextAlignmentCenter;
     NSLog(@"carsLabel = %@",carsLabel.text);
     //carsLabel.text = @"maybe because the string isn't long enough";
     carsLabel.textColor = [UIColor blackColor];
     carsLabel.font = font;





     carsLabel.opaque = YES;


     [view addSubview:carsLabel];

     return carsLabel; 
    } else {
     UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:18];

     UILabel *carsLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 225, 50)] autorelease];
     id fact = [self.facts objectAtIndex:(int)row];
     NSString *pickerText = @"Dictionary Entry";
     if ( [fact isKindOfClass:[NSString class]]) {


      pickerText = [self.facts objectAtIndex:(int)row];

     } 
     carsLabel.text = pickerText;
     carsLabel.textAlignment = UITextAlignmentCenter;
     NSLog(@"carsLabel = %@",carsLabel.text);
     //carsLabel.text = @"maybe because the string isn't long enough";
     carsLabel.textColor = [UIColor blackColor];
     carsLabel.font = font;
     if ( row == 0) {
     carsLabel.backgroundColor = [UIColor redColor];

     }
     //carsLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blackboard.png"]];;
     carsLabel.opaque = YES;

     [view addSubview:carsLabel];

     return carsLabel;
    }


    return nil;
}
A: 

It's unclear where you are putting the above code. Is it in -pickerView:viewForRow:forComponent:reusingView:? This is where it should be. Are you sure that you are maintaining a pointer to the label for that particular view? The fact that you are crashing suggests you probably are not. A larger block of code, including where you have put it, would be helpful.

Rob Napier
Rob, I've posted the full method thanks again for any suggestions!
ennuikiller
I think you've misunderstood how this delegate methods works. The pickerView is giving you a `reusingView`. You're modifying it with `-addSubView:`, but you're returning a different view (the `UILabel`). The first thing you should do here is get rid of the calls to `-addSubView:`.
Rob Napier
A: 

Call the UIPickerView instance's -viewForRow:forComponent: method to get the UIView * object. Then set that view's background UIColor.

Alex Reynolds
I beleive I've tried that already, but I've give it another go
ennuikiller
Also make sure you have set your UIView (containing the UIPickerView) as the pickerview's delegate, and that you have specified that the `UIView` will implement the `UIPickerViewDelegate` contract.
Alex Reynolds
Hi Alex,I've got the code above in the pickerviewcontroller and have set the pickerview.delegate = self in the loadView method. I tried in the didSelectRow method:UIView *selectedView = [numberFactsPicker viewForRow:numberRow forComponent:kFactComponent]; selectedView.backgroundColor = [UIColor redColor]; somehow I think I would need to add it back as a subview maybe?
ennuikiller
Maybe you can check if *selectedView is nil. Maybe something is not wired up right. Perhaps use the debugger or NSLog statements to check the values of things, as well as to make sure that your delegate methods are being called.
Alex Reynolds
Setting the viewForRow's background color works-mostly but, in the case of the first and last item, you can still see the picker's white background, "off the edge of the reel."Sadly, setting [myPickerView setBackGroundColor:[UIColor blackColor]] (or any color) doesn't do what one might hope.
Olie
+1  A: 
ennuikiller
A: 

The correct format for viewForPicker is:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    [label setText:@"Whatever"];

    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}

The problem with the code above is: it colorizes the labels, but not the picker, itself. So, when you roll to one end or the other, there's a blank spot where you can see the white background. Setting [myPicker setBackgroundColor...] doesn't do what one might hope.

Olie
A: 

Could you explain how you used your answer? Did you write your own viewForRow? Where did you place your answer code in your program?

georryan