我正在尝试自定义 UIPickerView 并且它大部分都在工作。但是,设置背景颜色仅适用于第一个组件(即第一列)。我似乎无法影响其他两列。我认为问题可能与 PickerView 中组件的加载顺序有关,但我不确定。任何帮助是极大的赞赏。
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//[(UIView*)[[_picker subviews] objectAtIndex:0] setAlpha:0.1f];
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
[self setColors];
NSLog(@"number of components %d",[pickerView numberOfComponents]);
if(component == 0)
{
//use Array1
[self setColors];
return [_pickerArray count];
}
else if (component==1)
{
[self setColors];
NSLog(@"component 1");
return [_pickerArray count];
} else if (component==2) {
NSLog(@"component 2");
[self setColors];
return [_pickerArray count];
} else {
return 3;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
//use Array1
return [_pickerArray objectAtIndex:row];
}
else
{
//use Array2
return [_pickerArrayTwo objectAtIndex:row];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
[self setColors];
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(0.0, -130.0, 60.0, 60.0)];
[label setBackgroundColor:[UIColor redColor]];
[label setTextColor:[UIColor blueColor]];
[label setFont:[UIFont boldSystemFontOfSize:40.0]];
[label setText:[NSString stringWithFormat:@"%d",row]];
label.backgroundColor = [UIColor clearColor];
return label;
}
-(void)setColors
{
[(UIView*)[[_picker subviews] objectAtIndex:0] setAlpha:0.0f];
[(UIView*)[[_picker subviews] objectAtIndex:1] setAlpha:0.0f];
[(UIView*)[[_picker subviews] objectAtIndex:2] setAlpha:0.0f];
[(UIView*)[[_picker subviews] objectAtIndex:3] setAlpha:0.0f];
// [(UIView*)[[_picker subviews] objectAtIndex:4] setAlpha:0.0f];
[(UIView*)[[_picker subviews] objectAtIndex:0] setHidden:YES];
// [(UIView*)[[_picker subviews] objectAtIndex:2] setAlpha:0.5f];
// NSLog(@"%@",[_picker subviews]);
_picker.backgroundColor = [UIColor clearColor];
}
谢谢您的帮助。