有人知道如何在iPhone SDK中更改UIPickerView控件中的行(或行背景)的颜色吗?类似于下面的行标题,但是我也想更改行的颜色:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
谢谢。
发布于 2008-11-22 03:18:44
您可以在委托的-pickerView:viewForRow:forComponent:reusingView:方法中返回任意视图,文档中记录了here。
发布于 2008-11-22 04:33:13
谢谢诺亚,这正是我需要的。我想在这里添加代码,以防其他人需要(或想要评论:)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
发布于 2012-08-24 06:27:03
我根据Sean的回答实现了以下内容:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
CGRect rowFrame = CGRectMake(00.0f, 0.0f, [pickerView viewForRow:row forComponent:component].frame.size.width, [pickerView viewForRow:row forComponent:component].frame.size.height);
UILabel *label = [[UILabel alloc] initWithFrame:rowFrame];
label.font = [UIFont boldSystemFontOfSize:18.0f];
// This is an array I pass to the picker in prepareForSegue:sender:
label.text = [self.values objectAtIndex:row];
label.textAlignment = UITextAlignmentCenter;
// This is an array I pass to the picker in prepareForSegue:sender:
if ([self.backgroundColors count]) {
label.backgroundColor = [self.backgroundColors objectAtIndex:row];
// self.lightColors is an array I instantiate in viewDidLoad: self.lightColors = @[ [UIColor yellowColor], [UIColor greenColor], [UIColor whiteColor] ];
label.textColor = [self.lightColors containsObject:label.backgroundColor] ? [UIColor blackColor] : [UIColor whiteColor];
} else {
label.textColor = [UIColor blackColor];
}
return label;
}
https://stackoverflow.com/questions/310786
复制相似问题