[英]how to change the picker view selected object colour in ios
How can change the selected element colour in uipickerview. 如何在uipickerview中更改所选元素的颜色。 have 10 elements in picker view they are all black colour .when selecting an one from 1 to 9 means the selected element want to show in blue colour.
在选择器视图中有10个元素,它们都是黑色。当从1到9中选择一个元素时,表示所选元素要以蓝色显示。 but the last element selected means it want to show red colour.
但是最后选择的元素表示它要显示红色。 how can i achieve this help me.
我如何才能做到这一点对我有帮助。
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@"1"];
[arrayNo addObject:@"2"];
[arrayNo addObject:@"3"];
[arrayNo addObject:@"4"];
[arrayNo addObject:@"5"];
[arrayNo addObject:@"6"];
[arrayNo addObject:@"7"];
[arrayNo addObject:@"8"];
[arrayNo addObject:@"9"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
UIView *view = [pickerView viewForRow:row forComponent:component];
UILabel *selectedLabel = (UILabel *) [view viewWithTag:1002];
if ([[arrayNo objectAtIndex:row] isEqual:[arrayNo lastObject]]) {
[selectedLabel setTextColor:[UIColor redColor]];
} else {
[selectedLabel setTextColor:[UIColor blueColor]];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrayNo count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (view) {
NSLog(@"view exists");
} else {
view = [[UIView alloc] initWithFrame:CGRectZero];
[view setTag:1001];
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(pickerView.frame), 30)];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setFont:[UIFont boldSystemFontOfSize:16]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTag:1002];
[lbl setTextColor:[UIColor blackColor]];
[lbl setText:[arrayNo objectAtIndex:row]];
[view addSubview:lbl];
}
return view;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
for(UIView *lview in pickerView.subviews) {
if ([lview isKindOfClass:[UILabel class]]) {
}
NSLog(@"%@",lview);
}
}
By using the above code you will get all the sub view and check wheather it is label or not. 通过使用上面的代码,您将获得所有子视图并检查是否为标签。
Then try to put different color to each label and then you find to your specific label, then you can do what ever you need. 然后尝试为每个标签加上不同的颜色,然后找到特定的标签,然后您便可以执行所需的操作。
The subviews order may change depending on the number of component in the picker.So, not added the exact logic. 子视图的顺序可能会根据选择器中组件的数量而变化。因此,未添加确切的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.