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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…