2014-03-26 66 views
0

我使用UIPickerView(例如)4行。第一個值是灰色文本的「選取值」。其他的是用戶應該用黑色文本選擇的值。(iOS)如何使UIPickerView中的第一個值不可選?

選擇是可選的,所以用戶可以跳過它。但如果他們不這樣做,在用戶開始採摘之後如何使第一個值不可選?

謝謝。 此致敬禮。

回答

1

使用UIPickerView委託方法- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component改變所選行,如果第一行被選中:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    if (row == 0) { 
     [pickerView selectRow:row+1 inComponent:component animated:YES]; 
    } 
} 

編輯:您可以以此來改變文字顏色:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { 

    if (row == 0) 
     return [[NSAttributedString alloc] initWithString:@"Pick a value" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}]; 
    else { 
     // Return titles 
    } 
} 

這是如果你的目標iOS 6+,它取代了以前使用的-pickerView:titleForRow:forComponent:方法。

+0

它的工作原理。還有一個問題 - 如何在該方法中將其變成灰色?如何製作一行的自定義文本和顏色? – Palindrome

+0

@Palindrome:我爲此編輯了我的問題 – rdurand

相關問題