[英]the color for the default selected row of the UIpickerView
我有一個CityPickerView,它有2個組件供用戶選擇中國城市。我希望所選行的顏色為紅色,未選擇的行為同性戀,同時,pickerView的背景顏色為白色。
我已完成主要功能,但在運行應用程序時, 默認選定行的顏色不是紅色。 怎么解決?
當用戶選擇組件0的行時,組件1的數據將自動重新加載,並且選擇組件1的第一行。 組件0的行和組件1的第一行都是紅色。
但是當用戶選擇組件1的第3行(或更多)時,則選擇組件0的另一行。組件0的選定行為紅色,但組件1的第一行不是紅色,它仍然是同性戀的顏色。 我該如何解決?
並且,如果用戶直接選擇組件1的行, 如何使組件0的行也是紅色?
UIPickerViewDelegate的代碼如下:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSUserDefaults *pickerSelectionDefaults=[NSUserDefaults standardUserDefaults];
[pickerSelectionDefaults setInteger:row forKey:@"leftComponentSelectionKey"];
[pickerSelectionDefaults setInteger:row forKey:@"rightComponentSelectionKey"];
[pickerSelectionDefaults synchronize];
UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
[labelSelected setTextColor:[UIColor redColor]];
if (component==0) {
[pickerView reloadComponent:1];//if any row of the component 0 is selected,then reload the data of the component 1
[pickerView selectRow:0 inComponent:1 animated:YES];//if the row of the component 0 is selected,then set the first row of the component 1 is selected
UILabel *label = (UILabel*)[pickerView viewForRow:0 forComponent:1];//get the label of the row 0 of component 1
[label setTextColor:[UIColor redColor]];
}
[self changeTextField];
}
和
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = (id)view;
if (!label){
label=[[UILabel alloc]init];
label.textAlignment = NSTextAlignmentCenter;
pickerView.backgroundColor=[UIColor whiteColor];
label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
label.textColor=[UIColor grayColor];
}
return label;
}
如果您需要更多詳細信息,我會在這里給出。謝謝。
您可以使用以下方法更改顏色:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//On Selecting the component row
if (component == 0) {
} else if (component == 1) {
[quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value
[pickerView reloadComponent:component]; //This will cause your viewForComp to hit
}
}
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
{
pickerLabel.textColor = defaultColor;
if([self.pickerView selectedRowInComponent:component] == row) //this is the selected one, change its color
{
pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1];
}
}
'animated'參數應為'false'
例如''pickerView.selectRow(row,inComponent:comp,animated:false)'
如果它是'真',那么挑選者最終會到達你手動選擇的行需要一些時間,延遲可以忽略顏色變化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.