好的,也许我错过了一些非常简单的东西,如果是这种情况,我深表歉意,但是,我已经用谷歌搜索了标题的所有排列,但没有找到!所以这就是我想要做的:在选择该行时更改我用作 2 组件选取器视图中的行视图的标签的背景颜色。所以我认为这会起作用:
if (row == [pickerview selectedRowForComponent])
viewlabel.backgroundColor = redcolor;
但这不起作用。它似乎随意选择要着色的行,有时甚至会出现错误的访问错误。我试过所有不同的条款都没有效果!任何建议将不胜感激!!
这是完整的方法:
- (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;
}