问题
I'm using a pickerView with multiple components related to several fields in a Database (CoreData). Is it possible to change the fontcolor for a specific component according the presence of data in the DB ? For example the field in the DB is null the component font color should be RED otherwise black.
Any help will be appreciated !
Dario
================== Thanks Kenny,
I have to apply to a single UIPicker only. So I', returning the view parametere (without modificatiosn). The result is all the pickers show empty rows.
Thanks for help !
Here you will find the code fragment:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView == tipoPk){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100,30)];
label.textColor = [UIColor redColor];
switch (component) {
case PK_Tipo:
label.text = [tipoArray objectAtIndex:row]];
break;
case PK_Settore:
label.text = [settoreArray objectAtIndex:row]];
break;
default:
break;
}
return label;
}
else {
return view; // <==== return view for non related pickerviews , but no rows shown
}
}
回答1:
This one also works..
- (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;
}
回答2:
Yes, but you have to use -pickerView:viewForRow:forComponent:reusingView: and supply a custom UILabel.
来源:https://stackoverflow.com/questions/2850234/uipickerview-change-font-color-according-data